@@ -40,6 +40,8 @@ import (
4040 "sigs.k8s.io/apiserver-network-proxy/proto/header"
4141)
4242
43+ const xfrChannelSize = 10
44+
4345type key int
4446
4547type ProxyClientConnection struct {
@@ -136,6 +138,9 @@ type ProxyServer struct {
136138 serverID string // unique ID of this server
137139 serverCount int // Number of proxy server instances, should be 1 unless it is a HA server.
138140
141+ // Allows a special debug flag which warns if we write to a full transfer channel
142+ warnOnChannelLimit bool
143+
139144 // agent authentication
140145 AgentAuthenticationOptions * AgentTokenAuthenticationOptions
141146
@@ -321,7 +326,7 @@ func (s *ProxyServer) getFrontendsForBackendConn(agentID string, backend Backend
321326}
322327
323328// NewProxyServer creates a new ProxyServer instance
324- func NewProxyServer (serverID string , proxyStrategies []ProxyStrategy , serverCount int , agentAuthenticationOptions * AgentTokenAuthenticationOptions ) * ProxyServer {
329+ func NewProxyServer (serverID string , proxyStrategies []ProxyStrategy , serverCount int , agentAuthenticationOptions * AgentTokenAuthenticationOptions , warnOnChannelLimit bool ) * ProxyServer {
325330 var bms []BackendManager
326331 for _ , ps := range proxyStrategies {
327332 switch ps {
@@ -343,9 +348,10 @@ func NewProxyServer(serverID string, proxyStrategies []ProxyStrategy, serverCoun
343348 serverCount : serverCount ,
344349 BackendManagers : bms ,
345350 AgentAuthenticationOptions : agentAuthenticationOptions ,
346- // use the first backendmanager as the Readiness Manager
347- Readiness : bms [0 ],
348- proxyStrategies : proxyStrategies ,
351+ // use the first backend-manager as the Readiness Manager
352+ Readiness : bms [0 ],
353+ proxyStrategies : proxyStrategies ,
354+ warnOnChannelLimit : warnOnChannelLimit ,
349355 }
350356}
351357
@@ -361,12 +367,13 @@ func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
361367 userAgent := md .Get (header .UserAgent )
362368 klog .V (2 ).InfoS ("proxy request from client" , "userAgent" , userAgent )
363369
364- recvCh := make (chan * client.Packet , 10 )
370+ recvCh := make (chan * client.Packet , xfrChannelSize )
365371 stopCh := make (chan error )
366372
367373 go s .serveRecvFrontend (stream , recvCh )
368374
369375 defer func () {
376+ klog .V (2 ).InfoS ("Receive channel on Proxy is stopping" , "userAgent" , userAgent , "serverID" , s .serverID )
370377 close (recvCh )
371378 }()
372379
@@ -375,6 +382,7 @@ func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
375382 for {
376383 in , err := stream .Recv ()
377384 if err == io .EOF {
385+ klog .V (2 ).InfoS ("Stream closed on Proxy" , "userAgent" , userAgent , "serverID" , s .serverID )
378386 close (stopCh )
379387 return
380388 }
@@ -384,6 +392,9 @@ func (s *ProxyServer) Proxy(stream client.ProxyService_ProxyServer) error {
384392 return
385393 }
386394
395+ if s .warnOnChannelLimit && len (recvCh ) >= xfrChannelSize {
396+ klog .V (2 ).InfoS ("Receive channel on Proxy is full" , "userAgent" , userAgent , "serverID" , s .serverID )
397+ }
387398 recvCh <- in
388399 }
389400 }()
@@ -410,13 +421,15 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
410421 // a new connection to the address.
411422 backend , err = s .getBackend (pkt .GetDialRequest ().Address )
412423 if err != nil {
413- klog .ErrorS (err , "Failed to get a backend" )
424+ klog .ErrorS (err , "Failed to get a backend" , "serverID" , s . serverID )
414425
415426 resp := & client.Packet {
416427 Type : client .PacketType_DIAL_RSP ,
417428 Payload : & client.Packet_DialResponse {DialResponse : & client.DialResponse {Error : err .Error ()}},
418429 }
419- stream .Send (resp )
430+ if err := stream .Send (resp ); err != nil {
431+ klog .V (5 ).Infoln ("Failed to send DIAL_RSP for no backend" , "error" , err , "serverID" , s .serverID )
432+ }
420433 // The Dial is failing; no reason to keep this goroutine.
421434 return
422435 }
@@ -430,29 +443,30 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
430443 backend : backend ,
431444 })
432445 if err := backend .Send (pkt ); err != nil {
433- klog .ErrorS (err , "DIAL_REQ to Backend failed" )
446+ klog .ErrorS (err , "DIAL_REQ to Backend failed" , "serverID" , s . serverID )
434447 }
435448 klog .V (5 ).Infoln ("DIAL_REQ sent to backend" ) // got this. but backend didn't receive anything.
436449
437450 case client .PacketType_CLOSE_REQ :
438451 connID := pkt .GetCloseRequest ().ConnectID
439452 klog .V (5 ).InfoS ("Received CLOSE_REQ" , "connectionID" , connID )
440453 if backend == nil {
441- klog .V (2 ).InfoS ("Backend has not been initialized for requested connection. Client should send a Dial Request first" , "connectionID" , connID )
454+ klog .V (2 ).InfoS ("Backend has not been initialized for requested connection. Client should send a Dial Request first" ,
455+ "serverID" , s .serverID , "connectionID" , connID )
442456 continue
443457 }
444458 if err := backend .Send (pkt ); err != nil {
445459 // TODO: retry with other backends connecting to this agent.
446- klog .ErrorS (err , "CLOSE_REQ to Backend failed" )
460+ klog .ErrorS (err , "CLOSE_REQ to Backend failed" , "serverID" , s . serverID , "connectionID" , connID )
447461 }
448- klog .V (5 ).Infoln ("CLOSE_REQ sent to backend" )
462+ klog .V (5 ).Infoln ("CLOSE_REQ sent to backend" , "serverID" , s . serverID , "connectionID" , connID )
449463
450464 case client .PacketType_DIAL_CLS :
451465 random := pkt .GetCloseDial ().Random
452- klog .V (5 ).InfoS ("Received DIAL_CLOSE" , "random" , random )
466+ klog .V (5 ).InfoS ("Received DIAL_CLOSE" , "serverID" , s . serverID , " random" , random )
453467 // Currently not worrying about backend as we do not have an established connection,
454468 s .PendingDial .Remove (random )
455- klog .V (5 ).Infoln ("Removing pending dial request" , "random" , random )
469+ klog .V (5 ).Infoln ("Removing pending dial request" , "serverID" , s . serverID , " random" , random )
456470
457471 case client .PacketType_DATA :
458472 connID := pkt .GetData ().ConnectID
@@ -470,17 +484,18 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
470484 }
471485 if err := backend .Send (pkt ); err != nil {
472486 // TODO: retry with other backends connecting to this agent.
473- klog .ErrorS (err , "DATA to Backend failed" )
487+ klog .ErrorS (err , "DATA to Backend failed" , "serverID" , s . serverID , "connectionID" , connID )
474488 continue
475489 }
476490 klog .V (5 ).Infoln ("DATA sent to Backend" )
477491
478492 default :
479- klog .V (5 ).InfoS ("Ignore packet coming from frontend" , "type" , pkt .Type )
493+ klog .V (5 ).InfoS ("Ignore packet coming from frontend" ,
494+ "type" , pkt .Type , "serverID" , s .serverID , "connectionID" , firstConnID )
480495 }
481496 }
482497
483- klog .V (5 ).InfoS ("Close streaming" , "connectionID" , firstConnID )
498+ klog .V (5 ).InfoS ("Close streaming" , "serverID" , s . serverID , " connectionID" , firstConnID )
484499
485500 pkt := & client.Packet {
486501 Type : client .PacketType_CLOSE_REQ ,
@@ -496,7 +511,7 @@ func (s *ProxyServer) serveRecvFrontend(stream client.ProxyService_ProxyServer,
496511 return
497512 }
498513 if err := backend .Send (pkt ); err != nil {
499- klog .ErrorS (err , "CLOSE_REQ to Backend failed" )
514+ klog .ErrorS (err , "CLOSE_REQ to Backend failed" , "serverID" , s . serverID )
500515 }
501516}
502517
@@ -617,24 +632,26 @@ func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
617632
618633 if s .AgentAuthenticationOptions .Enabled {
619634 if err := s .authenticateAgentViaToken (stream .Context ()); err != nil {
620- klog .ErrorS (err , "Client authentication failed" )
635+ klog .ErrorS (err , "Client authentication failed" , "agentID" , agentID )
621636 return err
622637 }
623638 }
624639
625640 h := metadata .Pairs (header .ServerID , s .serverID , header .ServerCount , strconv .Itoa (s .serverCount ))
626641 if err := stream .SendHeader (h ); err != nil {
642+ klog .ErrorS (err , "Failed to send server count back to agent" , "agentID" , agentID )
627643 return err
628644 }
629645
630646 backend := s .addBackend (agentID , stream )
631647 defer s .removeBackend (agentID , stream )
632648
633- recvCh := make (chan * client.Packet , 10 )
649+ recvCh := make (chan * client.Packet , xfrChannelSize )
634650
635651 go s .serveRecvBackend (backend , stream , agentID , recvCh )
636652
637653 defer func () {
654+ klog .V (2 ).InfoS ("Receive channel on Connect is stopping" , "agentID" , agentID , "serverID" , s .serverID )
638655 close (recvCh )
639656 }()
640657
@@ -643,6 +660,7 @@ func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
643660 for {
644661 in , err := stream .Recv ()
645662 if err == io .EOF {
663+ klog .V (2 ).InfoS ("Stream closed on Connect" , "agentID" , agentID , "serverID" , s .serverID )
646664 close (stopCh )
647665 return
648666 }
@@ -652,6 +670,9 @@ func (s *ProxyServer) Connect(stream agent.AgentService_ConnectServer) error {
652670 return
653671 }
654672
673+ if s .warnOnChannelLimit && len (recvCh ) >= xfrChannelSize {
674+ klog .V (2 ).InfoS ("Receive channel on Connect is full" , "agentID" , agentID , "serverID" , s .serverID )
675+ }
655676 recvCh <- in
656677 }
657678 }()
@@ -666,7 +687,8 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
666687 // TODO(#126): Frontends in PendingDial state that have not been added to the
667688 // list of frontends should also be closed.
668689 frontends , _ := s .getFrontendsForBackendConn (agentID , backend )
669- klog .V (3 ).InfoS ("Close frontends connected to agent" , "count" , len (frontends ), "agentID" , agentID )
690+ klog .V (3 ).InfoS ("Close frontends connected to agent" ,
691+ "serverID" , s .serverID , "count" , len (frontends ), "agentID" , agentID )
670692
671693 for _ , frontend := range frontends {
672694 s .removeFrontend (agentID , frontend .connectID )
@@ -678,7 +700,7 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
678700 }
679701 pkt .GetCloseResponse ().ConnectID = frontend .connectID
680702 if err := frontend .send (pkt ); err != nil {
681- klog .ErrorS (err , "CLOSE_RSP to frontend failed" )
703+ klog .ErrorS (err , "CLOSE_RSP to frontend failed" , "serverID" , s . serverID , "agentID" , agentID )
682704 }
683705 }
684706 }()
@@ -690,17 +712,18 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
690712 klog .V (5 ).InfoS ("Received DIAL_RSP" , "random" , resp .Random , "agentID" , agentID , "connectionID" , resp .ConnectID )
691713
692714 if frontend , ok := s .PendingDial .Get (resp .Random ); ! ok {
693- klog .V (5 ).Infoln ("DIAL_RSP not recognized; dropped" )
715+ klog .V (2 ).Infoln ("DIAL_RSP not recognized; dropped" , "random" , resp . Random , "agentID" , agentID , "connectionID" , resp . ConnectID )
694716 } else {
695717 dialErr := false
696718 if resp .Error != "" {
697- klog .ErrorS (errors .New (resp .Error ), "DIAL_RSP contains failure" )
719+ klog .ErrorS (errors .New (resp .Error ), "DIAL_RSP contains failure" , "random" , resp . Random , "agentID" , agentID , "connectionID" , resp . ConnectID )
698720 dialErr = true
699721 }
700722 err := frontend .send (pkt )
701723 s .PendingDial .Remove (resp .Random )
702724 if err != nil {
703- klog .ErrorS (err , "DIAL_RSP send to frontend stream failure" )
725+ klog .ErrorS (err , "DIAL_RSP send to frontend stream failure" ,
726+ "random" , resp .Random , "serverID" , s .serverID , "agentID" , agentID , "connectionID" , resp .ConnectID )
704727 dialErr = true
705728 }
706729 // Avoid adding the frontend if there was an error dialing the destination
@@ -719,35 +742,35 @@ func (s *ProxyServer) serveRecvBackend(backend Backend, stream agent.AgentServic
719742 klog .V (5 ).InfoS ("Received data from agent" , "bytes" , len (resp .Data ), "agentID" , agentID , "connectionID" , resp .ConnectID )
720743 frontend , err := s .getFrontend (agentID , resp .ConnectID )
721744 if err != nil {
722- klog .ErrorS (err , "could not get frontend client" , "connectionID" , resp .ConnectID )
745+ klog .ErrorS (err , "could not get frontend client" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
723746 break
724747 }
725748 if err := frontend .send (pkt ); err != nil {
726- klog .ErrorS (err , "send to client stream failure" )
749+ klog .ErrorS (err , "send to client stream failure" , "serverID" , s . serverID , "agentID" , agentID , "connectionID" , resp . ConnectID )
727750 } else {
728751 klog .V (5 ).InfoS ("DATA sent to frontend" )
729752 }
730753
731754 case client .PacketType_CLOSE_RSP :
732755 resp := pkt .GetCloseResponse ()
733- klog .V (5 ).InfoS ("Received CLOSE_RSP" , "connectionID" , resp .ConnectID )
756+ klog .V (5 ).InfoS ("Received CLOSE_RSP" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
734757 frontend , err := s .getFrontend (agentID , resp .ConnectID )
735758 if err != nil {
736- klog .ErrorS (err , "could not get frontend client" , "connectionID" , resp .ConnectID )
759+ klog .ErrorS (err , "could not get frontend client" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
737760 break
738761 }
739762 if err := frontend .send (pkt ); err != nil {
740763 // Normal when frontend closes it.
741- klog .ErrorS (err , "CLOSE_RSP send to client stream error" , "connectionID" , resp .ConnectID )
764+ klog .ErrorS (err , "CLOSE_RSP send to client stream error" , "serverID" , s . serverID , "agentID" , agentID , " connectionID" , resp .ConnectID )
742765 } else {
743766 klog .V (5 ).Infoln ("CLOSE_RSP sent to frontend" , "connectionID" , resp .ConnectID )
744767 }
745768 s .removeFrontend (agentID , resp .ConnectID )
746769 klog .V (5 ).InfoS ("Close streaming" , "agentID" , agentID , "connectionID" , resp .ConnectID )
747770
748771 default :
749- klog .V (2 ).InfoS ("Unrecognized packet" , "packet" , pkt )
772+ klog .V (2 ).InfoS ("Unrecognized packet" , "packet" , pkt , "serverID" , s . serverID , "agentID" , agentID )
750773 }
751774 }
752- klog .V (5 ).InfoS ("Close backend of agent" , "backend" , stream , "agentID" , agentID )
775+ klog .V (5 ).InfoS ("Close backend of agent" , "backend" , stream , "serverID" , s . serverID , " agentID" , agentID )
753776}
0 commit comments