Skip to content

小白过来问个问题 #16

Description

@rebornwwp
func (proxy *EasyProxy) Dispatch(conn net.Conn) {
	if proxy.isBackendAvailable() {
		servers := proxy.data.BackendUrls()
                // 这里不是特别懂,为啥选出这个url呢?
		url := proxy.strategy.Choose(conn.RemoteAddr().String(), servers)
		proxy.transfer(conn, url)
	} else {
		conn.Close()
		log.Println("no backend available now,please check your server!")
	}
}

func (proxy *EasyProxy) transfer(local net.Conn, remote string) {
	remoteConn, err := net.DialTimeout("tcp", remote, DefaultTimeOut*time.Second)
	if err != nil {
		local.Close()
		proxy.Clean(remote)
		log.Println("connect backend err", err)
		return
	}
	sync := make(chan int)
	channel := &structure.Channel{Src: local, Dst: remoteConn}
	go proxy.putChannel(channel)
	go proxy.safeCopy(local, remoteConn, sync)
	go proxy.safeCopy(remoteConn, local, sync)
	go proxy.closeChannel(channel, sync)
}

我的理解,proxy.strategy.Choose 会在backend随机选择一个对应的backend server来做连接,为啥这里strategy是随机的,不是应该和conn对应对应的吗? 谢谢大佬帮忙解答一下

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions