@@ -756,18 +756,19 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
756
756
klog .V (5 ).InfoS ("Received data from agent" , "bytes" , len (resp .Data ), "agentID" , agentID , "connectionID" , resp .ConnectID )
757
757
kasConnCtx , isFromAgent := s .forwardConnectionManager .Get (resp .ConnectID )
758
758
if isFromAgent {
759
- kasConnCtx . dataCh <- resp . Data
759
+ s . handleDataPacketFromAgent ( resp , kasConnCtx )
760
760
continue
761
- }
762
- frontend , err := s .getFrontend (agentID , resp .ConnectID )
763
- if err != nil {
764
- klog .ErrorS (err , "could not get frontend client" , "connectionID" , resp .ConnectID )
765
- break
766
- }
767
- if err := frontend .send (pkt ); err != nil {
768
- klog .ErrorS (err , "send to client stream failure" , "serverID" , s .serverID , "agentID" , agentID , "connectionID" , resp .ConnectID )
769
761
} else {
770
- klog .V (5 ).InfoS ("DATA sent to frontend" )
762
+ frontend , err := s .getFrontend (agentID , resp .ConnectID )
763
+ if err != nil {
764
+ klog .ErrorS (err , "could not get frontend client" , "connectionID" , resp .ConnectID )
765
+ break
766
+ }
767
+ if err := frontend .send (pkt ); err != nil {
768
+ klog .ErrorS (err , "send to client stream failure" , "serverID" , s .serverID , "agentID" , agentID , "connectionID" , resp .ConnectID )
769
+ } else {
770
+ klog .V (5 ).InfoS ("DATA sent to frontend" )
771
+ }
771
772
}
772
773
773
774
case client .PacketType_CLOSE_RSP :
@@ -830,10 +831,8 @@ func (s *ProxyServer) handleDialRequest(pkt *client.Packet, backend Backend) {
830
831
// Odd identifiers are used for connections from node to master network,
831
832
// increment by 2 to maintain the invariant.
832
833
connID := atomic .AddInt64 (& s .nextConnID , 2 )
833
- dataCh := make (chan []byte , 5 )
834
834
kasConnCtx := & connContext {
835
- conn : conn ,
836
- dataCh : dataCh ,
835
+ conn : conn ,
837
836
cleanFunc : func () {
838
837
klog .V (4 ).InfoS ("close connection" , "connectionID" , connID )
839
838
req := & client.Packet {
@@ -853,7 +852,6 @@ func (s *ProxyServer) handleDialRequest(pkt *client.Packet, backend Backend) {
853
852
klog .ErrorS (err , "error occurred while closing connection" , "connectionID" , connID )
854
853
}
855
854
856
- close (dataCh )
857
855
s .forwardConnectionManager .Delete (connID )
858
856
},
859
857
backend : backend ,
@@ -869,13 +867,11 @@ func (s *ProxyServer) handleDialRequest(pkt *client.Packet, backend Backend) {
869
867
870
868
// proxy data to and from KAS for the connection
871
869
go s .agentToProxy (connID , kasConnCtx )
872
- go s .proxyToAgent (connID , kasConnCtx )
873
870
}
874
871
875
872
type connContext struct {
876
873
conn net.Conn
877
874
cleanFunc func ()
878
- dataCh chan []byte
879
875
cleanOnce sync.Once
880
876
backend Backend
881
877
}
@@ -915,24 +911,22 @@ func (s *ProxyServer) agentToProxy(connID int64, ctx *connContext) {
915
911
}
916
912
}
917
913
918
- func (s * ProxyServer ) proxyToAgent (connID int64 , ctx * connContext ) {
919
- defer ctx .cleanup ()
920
-
921
- for d := range ctx .dataCh {
922
- pos := 0
923
- for {
924
- n , err := ctx .conn .Write (d [pos :])
925
- if err == nil {
926
- klog .V (4 ).InfoS ("write to remote" , "connectionID" , connID , "lastData" , n )
927
- break
928
- } else if n > 0 {
929
- // https://golang.org/pkg/io/#Writer specifies return non nil error if n < len(d)
930
- klog .ErrorS (err , "write to remote with failure" , "connectionID" , connID , "lastData" , n )
931
- pos += n
932
- } else {
933
- klog .ErrorS (err , "conn write failure" , "connectionID" , connID )
934
- return
935
- }
914
+ func (s * ProxyServer ) handleDataPacketFromAgent (resp * client.Data , kasConnCtx * connContext ) {
915
+ pos := 0
916
+ connID := resp .ConnectID
917
+ d := resp .Data
918
+ for {
919
+ n , err := kasConnCtx .conn .Write (d [pos :])
920
+ if err == nil {
921
+ klog .V (4 ).InfoS ("write to remote" , "connectionID" , connID , "lastData" , n )
922
+ break
923
+ } else if n > 0 {
924
+ // https://golang.org/pkg/io/#Writer specifies return non nil error if n < len(d)
925
+ klog .ErrorS (err , "write to remote with failure" , "connectionID" , connID , "lastData" , n )
926
+ pos += n
927
+ } else {
928
+ klog .ErrorS (err , "conn write failure" , "connectionID" , connID )
929
+ return
936
930
}
937
931
}
938
932
}
0 commit comments