@@ -269,31 +269,28 @@ func (ocm *outgoingConnManager) run(ch chan<- outgoingConn) {
269269 for {
270270 switch state {
271271 case bgp .BGP_FSM_CONNECT :
272- connCh := make (chan net.Conn , 1 )
273- var wg sync.WaitGroup
274- wg .Add (1 )
275- go ocm .fsm .h .connectLoop (ocm .ctx , connCh , & wg )
276- select {
277- case <- ocm .ctx .Done ():
278- wg .Wait ()
272+ conn = ocm .fsm .h .connectLoop (ocm .ctx )
273+ if ocm .ctx .Err () != nil {
274+ // right after connectLoop() returns a connection, the context may be canceled.
275+ if conn != nil {
276+ conn .Close ()
277+ }
279278 return
280- case conn = <- connCh :
281- wg .Wait ()
282- initializeConn (fsm , conn )
279+ }
280+ initializeConn (fsm , conn )
283281
284- fsm .lock .Lock ()
285- open := buildopen (fsm .gConf , fsm .pConf )
286- fsm .lock .Unlock ()
287- b , _ := open .Serialize ()
282+ fsm .lock .Lock ()
283+ open := buildopen (fsm .gConf , fsm .pConf )
284+ fsm .lock .Unlock ()
285+ b , _ := open .Serialize ()
288286
289- if _ , err := conn .Write (b ); err != nil {
290- conn .Close ()
291- continue
292- }
293- fsm .bgpMessageStateUpdate (bgp .BGP_MSG_OPEN , false )
294- fsm .logger .Debug ("outgoing connection established" )
295- state = bgp .BGP_FSM_OPENSENT
287+ if _ , err := conn .Write (b ); err != nil {
288+ conn .Close ()
289+ continue
296290 }
291+ fsm .bgpMessageStateUpdate (bgp .BGP_MSG_OPEN , false )
292+ fsm .logger .Debug ("outgoing connection established" )
293+ state = bgp .BGP_FSM_OPENSENT
297294 case bgp .BGP_FSM_OPENSENT :
298295 recvCh := make (chan * fsmMsg , 1 )
299296 reasonCh := make (chan fsmStateReason , 1 )
@@ -804,8 +801,7 @@ func (h *fsmHandler) idle(ctx context.Context) (bgp.FSMState, *fsmStateReason) {
804801 }
805802}
806803
807- func (h * fsmHandler ) connectLoop (ctx context.Context , connCh chan <- net.Conn , wg * sync.WaitGroup ) {
808- defer wg .Done ()
804+ func (h * fsmHandler ) connectLoop (ctx context.Context ) net.Conn {
809805 fsm := h .fsm
810806
811807 retryInterval , addr , port , password , ttl , ttlMin , mss , localAddress , localPort , bindInterface := func () (int , string , int , string , uint8 , uint8 , uint16 , string , int , string ) {
@@ -842,7 +838,7 @@ func (h *fsmHandler) connectLoop(ctx context.Context, connCh chan<- net.Conn, wg
842838 case <- ctx .Done ():
843839 fsm .logger .Debug ("stop connect loop" )
844840 timer .Stop ()
845- return
841+ return nil
846842 case <- timer .C :
847843 fsm .logger .Debug ("try to connect" )
848844 }
@@ -866,17 +862,12 @@ func (h *fsmHandler) connectLoop(ctx context.Context, connCh chan<- net.Conn, wg
866862 select {
867863 case <- ctx .Done ():
868864 fsm .logger .Debug ("stop connect loop" )
869- return
865+ return nil
870866 default :
871867 }
872868
873869 if err == nil {
874- if nonblockSendChannel (connCh , conn ) {
875- return
876- } else {
877- conn .Close ()
878- fsm .logger .Warn ("active conn is closed to avoid being blocked" )
879- }
870+ return conn
880871 } else {
881872 fsm .logger .Debug ("failed to connect" , slog .String ("Error" , err .Error ()))
882873 }
0 commit comments