From 13f6493813b8f9a23b57efb595d4f2a523a35390 Mon Sep 17 00:00:00 2001 From: tycho garen Date: Wed, 15 Jun 2022 09:31:21 -0400 Subject: [PATCH] add max dialing attempts --- internal/p2p/peermanager.go | 15 +++++++++++++++ node/setup.go | 1 + 2 files changed, 16 insertions(+) diff --git a/internal/p2p/peermanager.go b/internal/p2p/peermanager.go index 6dd045efb..9ecb104a7 100644 --- a/internal/p2p/peermanager.go +++ b/internal/p2p/peermanager.go @@ -148,6 +148,10 @@ type PeerManagerOptions struct { // retry times, to avoid thundering herds. 0 disables jitter. RetryTimeJitter time.Duration + // Maximum number of times we will try to dial a peer before + // marking it inactive. + MaxFailedDialAttempts uint32 + // PeerScores sets fixed scores for specific peers. It is mainly used // for testing. A score of 0 is ignored. PeerScores map[types.NodeID]PeerScore @@ -565,6 +569,17 @@ func (m *PeerManager) DialFailed(address NodeAddress) error { addressInfo.LastDialFailure = time.Now().UTC() addressInfo.DialFailures++ + + var totalDialFailures uint32 + for _, addr := range peer.AddressInfo { + totalDialFailures += addr.DialFailures + } + + if m.options.MaxFailedDialAttempts > 0 && totalDialFailures > m.options.MaxFailedDialAttempts { + peer.Inactive = true + m.metrics.PeersInactivated.Add(1) + } + if err := m.store.Set(peer); err != nil { return err } diff --git a/node/setup.go b/node/setup.go index 76648b598..4b6065605 100644 --- a/node/setup.go +++ b/node/setup.go @@ -499,6 +499,7 @@ func createPeerManager( SelfAddress: selfAddr, MaxConnected: maxConns, MaxConnectedUpgrade: maxUpgradeConns, + MaxFailedDialAttempts: 1024, MaxPeers: maxUpgradeConns + 2*maxConns, MinRetryTime: 250 * time.Millisecond, MaxRetryTime: 30 * time.Minute,