// startTcpServer implement L4 proxy to the real server
startTcpServer implement L4 proxy to the real server
// startTcpServer implement L4 proxy to the real server
func startTcpServer(conn net.Conn, svcName string) {
	portString := strings.Split(conn.LocalAddr().String(), ":")
	localPort, _ := strconv.ParseInt(portString[1], 10, 32)
	addr, err := doLoadBalance(svcName, localPort)
	if err != nil {
		klog.Warningf("[L4 Proxy] %s call svc : %s encountered an error: %s", conn.RemoteAddr().String(), svcName, err)
		conn.Close()
		return
	}
	var proxyClient net.Conn
	for retry := 0; retry < defaultTcpReconnectTimes; retry++ {
		proxyClient, err = net.DialTimeout("tcp", addr.String(), defaultTcpClientTimeout)
		if err == nil {
			break
		}
	}
	if err != nil {
		klog.Warningf("[L4 Proxy] %s call svc : %s to %s encountered an error: %s", conn.RemoteAddr().String(), svcName, addr.String(), err)
		conn.Close()
		return
	}
	ctk := &conntrack{
		lconn: conn,
		rconn: proxyClient,
	}
	klog.Infof("[L4 Proxy] start a proxy server : %s,%s", svcName, addr.String())
	go func() {
		ctk.processServerProxy()
	}()
	go func() {
		ctk.processClientProxy()
	}()
}
func startTcpServer ( conn net . Conn , svcName string ) { portString := strings . Split ( conn . LocalAddr ( ) . String ( ) , " " ) localPort , _ := strconv . ParseInt ( portString [ 1 ] , 10 , 32 ) addr , err := doLoadBalance ( svcName , localPort ) if err != nil { klog . Warningf ( " " , conn . RemoteAddr ( ) . String ( ) , svcName , err ) conn . Close ( ) return } var proxyClient net . Conn for retry := 0 ; retry < defaultTcpReconnectTimes ; retry ++ { proxyClient , err = net . DialTimeout ( " " , addr . String ( ) , defaultTcpClientTimeout ) if err == nil { break } } if err != nil { klog . Warningf ( " " , conn . RemoteAddr ( ) . String ( ) , svcName , addr . String ( ) , err ) conn . Close ( ) return } ctk := & conntrack { lconn : conn , rconn : proxyClient , } klog . Infof ( " " , svcName , addr . String ( ) ) go func ( ) { ctk . processServerProxy ( ) } ( ) go func ( ) { ctk . processClientProxy ( ) } ( ) }
startTcpServer implement L4 proxy to the real server
startTcpServer implement L4 proxy to the real server
// portString is a standard form, such as "172.17.0.1:8080" // Error when connecting to server ,maybe timeout or any other error