server: use one func for peer re-connection

Let the connectToPersistentPeer func handle all connection attempts to
persistent peers so that that logic is in once place.
This commit is contained in:
Elle Mouton
2021-11-03 15:02:23 +02:00
parent 789f00746b
commit b9b7ea2a6a

View File

@@ -2798,34 +2798,24 @@ func (s *server) establishPersistentConnections() error {
IdentityKey: nodeAddr.pubKey, IdentityKey: nodeAddr.pubKey,
Address: address, Address: address,
} }
srvrLog.Debugf("Attempting persistent connection to "+
"channel peer %v", lnAddr)
// Send the persistent connection request to the s.persistentPeerAddrs[pubStr] = append(
// connection manager, saving the request itself so we s.persistentPeerAddrs[pubStr], lnAddr)
// can cancel/restart the process as needed. }
connReq := &connmgr.ConnReq{
Addr: lnAddr,
Permanent: true,
}
s.persistentConnReqs[pubStr] = append( // We'll connect to the first 10 peers immediately, then
s.persistentConnReqs[pubStr], connReq) // randomly stagger any remaining connections if the
// stagger initial reconnect flag is set. This ensures
// that mobile nodes or nodes with a small number of
// channels obtain connectivity quickly, but larger
// nodes are able to disperse the costs of connecting to
// all peers at once.
if numOutboundConns < numInstantInitReconnect ||
!s.cfg.StaggerInitialReconnect {
// We'll connect to the first 10 peers immediately, then go s.connectToPersistentPeer(pubStr)
// randomly stagger any remaining connections if the } else {
// stagger initial reconnect flag is set. This ensures go s.delayInitialReconnect(pubStr)
// that mobile nodes or nodes with a small number of
// channels obtain connectivity quickly, but larger
// nodes are able to disperse the costs of connecting to
// all peers at once.
if numOutboundConns < numInstantInitReconnect ||
!s.cfg.StaggerInitialReconnect {
go s.connMgr.Connect(connReq)
} else {
go s.delayInitialReconnect(connReq)
}
} }
numOutboundConns++ numOutboundConns++
@@ -2834,16 +2824,15 @@ func (s *server) establishPersistentConnections() error {
return nil return nil
} }
// delayInitialReconnect will attempt a reconnection using the passed connreq // delayInitialReconnect will attempt a reconnection to the given peer after
// after sampling a value for the delay between 0s and the // sampling a value for the delay between 0s and the maxInitReconnectDelay.
// maxInitReconnectDelay.
// //
// NOTE: This method MUST be run as a goroutine. // NOTE: This method MUST be run as a goroutine.
func (s *server) delayInitialReconnect(connReq *connmgr.ConnReq) { func (s *server) delayInitialReconnect(pubStr string) {
delay := time.Duration(prand.Intn(maxInitReconnectDelay)) * time.Second delay := time.Duration(prand.Intn(maxInitReconnectDelay)) * time.Second
select { select {
case <-time.After(delay): case <-time.After(delay):
s.connMgr.Connect(connReq) s.connectToPersistentPeer(pubStr)
case <-s.quit: case <-s.quit:
} }
} }
@@ -3822,9 +3811,16 @@ func (s *server) connectToPersistentPeer(pubKeyStr string) {
Permanent: true, Permanent: true,
} }
srvrLog.Debugf("Attempting persistent connection to "+
"channel peer %v", addr)
// Send the persistent connection request to the connection
// manager, saving the request itself so we can cancel/restart
// the process as needed.
s.persistentConnReqs[pubKeyStr] = append( s.persistentConnReqs[pubKeyStr] = append(
s.persistentConnReqs[pubKeyStr], connReq, s.persistentConnReqs[pubKeyStr], connReq,
) )
go s.connMgr.Connect(connReq) go s.connMgr.Connect(connReq)
} }
} }