EasyProxy icon indicating copy to clipboard operation
EasyProxy copied to clipboard

小白过来问个问题

Open rebornwwp opened this issue 6 years ago • 0 comments

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对应对应的吗? 谢谢大佬帮忙解答一下

rebornwwp avatar May 13 '19 13:05 rebornwwp