Skip to content

Commit 25d2b1b

Browse files
authored
Merge pull request #2885 from cfromknecht/stagger-initial-reconnect
server: stagger initial reconnects
2 parents caa0e2f + cf80476 commit 25d2b1b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

config.go

+2
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,8 @@ type config struct {
256256

257257
RejectPush bool `long:"rejectpush" description:"If true, lnd will not accept channel opening requests with non-zero push amounts. This should prevent accidental pushes to merchant nodes."`
258258

259+
StaggerInitialReconnect bool `long:"stagger-initial-reconnect" description:"If true, will apply a randomized staggering between 0s and 30s when reconnecting to persistent peers on startup. The first 10 reconnections will be attempted instantly, regardless of the flag's value"`
260+
259261
net tor.Net
260262

261263
Routing *routing.Conf `group:"routing" namespace:"routing"`

server.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"fmt"
99
"image/color"
1010
"math/big"
11+
prand "math/rand"
1112
"net"
1213
"path/filepath"
1314
"regexp"
@@ -60,6 +61,18 @@ const (
6061
// durations exceeding this value will be eligible to have their
6162
// backoffs reduced.
6263
defaultStableConnDuration = 10 * time.Minute
64+
65+
// numInstantInitReconnect specifies how many persistent peers we should
66+
// always attempt outbound connections to immediately. After this value
67+
// is surpassed, the remaining peers will be randomly delayed using
68+
// maxInitReconnectDelay.
69+
numInstantInitReconnect = 10
70+
71+
// maxInitReconnectDelay specifies the maximum delay in seconds we will
72+
// apply in attempting to reconnect to persistent peers on startup. The
73+
// value used or a particular peer will be chosen between 0s and this
74+
// value.
75+
maxInitReconnectDelay = 30
6376
)
6477

6578
var (
@@ -1932,6 +1945,7 @@ func (s *server) establishPersistentConnections() error {
19321945

19331946
// Iterate through the combined list of addresses from prior links and
19341947
// node announcements and attempt to reconnect to each node.
1948+
var numOutboundConns int
19351949
for pubStr, nodeAddr := range nodeAddrsMap {
19361950
// Add this peer to the set of peers we should maintain a
19371951
// persistent connection with.
@@ -1962,13 +1976,42 @@ func (s *server) establishPersistentConnections() error {
19621976
s.persistentConnReqs[pubStr] = append(
19631977
s.persistentConnReqs[pubStr], connReq)
19641978

1965-
go s.connMgr.Connect(connReq)
1979+
// We'll connect to the first 10 peers immediately, then
1980+
// randomly stagger any remaining connections if the
1981+
// stagger initial reconnect flag is set. This ensures
1982+
// that mobile nodes or nodes with a small number of
1983+
// channels obtain connectivity quickly, but larger
1984+
// nodes are able to disperse the costs of connecting to
1985+
// all peers at once.
1986+
if numOutboundConns < numInstantInitReconnect ||
1987+
!cfg.StaggerInitialReconnect {
1988+
1989+
go s.connMgr.Connect(connReq)
1990+
} else {
1991+
go s.delayInitialReconnect(connReq)
1992+
}
19661993
}
1994+
1995+
numOutboundConns++
19671996
}
19681997

19691998
return nil
19701999
}
19712000

2001+
// delayInitialReconnect will attempt a reconnection using the passed connreq
2002+
// after sampling a value for the delay between 0s and the
2003+
// maxInitReconnectDelay.
2004+
//
2005+
// NOTE: This method MUST be run as a goroutine.
2006+
func (s *server) delayInitialReconnect(connReq *connmgr.ConnReq) {
2007+
delay := time.Duration(prand.Intn(maxInitReconnectDelay)) * time.Second
2008+
select {
2009+
case <-time.After(delay):
2010+
s.connMgr.Connect(connReq)
2011+
case <-s.quit:
2012+
}
2013+
}
2014+
19722015
// prunePersistentPeerConnection removes all internal state related to
19732016
// persistent connections to a peer within the server. This is used to avoid
19742017
// persistent connection retries to peers we do not have any open channels with.

0 commit comments

Comments
 (0)