Skip to content

Commit

Permalink
p2p: reduce peer score for dial failures (backport #7265) (#7271)
Browse files Browse the repository at this point in the history
* p2p: reduce peer score for dial failures (#7265)

When dialing fails to succeed we should reduce the score of the peer,
which puts the peer at (potentially) greater chances of being removed
from the peer manager, and reduces the chance of the peer being
gossiped by the PEX reactor.

(cherry picked from commit 27560cf)

Co-authored-by: Sam Kleinman <[email protected]>
(cherry picked from commit 37e0779)
  • Loading branch information
mergify[bot] authored and lklimek committed Mar 25, 2022
1 parent 6850ed7 commit ef5886e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Special thanks to external contributors on this release:

- P2P Protocol

- [p2p] \#7265 Peer manager reduces peer score for each failed dial attempts for peers that have not successfully dialed. (@tychoish)

- Go API

- Blockchain Protocol
Expand Down
21 changes: 18 additions & 3 deletions internal/p2p/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ func (m *PeerManager) DialFailed(address NodeAddress) error {
if !ok {
return nil // Assume the address has been removed, ignore.
}

addressInfo.LastDialFailure = time.Now().UTC()
addressInfo.DialFailures++
if err := m.store.Set(peer); err != nil {
Expand Down Expand Up @@ -614,6 +615,7 @@ func (m *PeerManager) Dialed(address NodeAddress, peerOpts ...func(*peerInfo)) e
addressInfo.LastDialSuccess = now
// If not found, assume address has been removed.
}

if err := m.store.Set(peer); err != nil {
return err
}
Expand Down Expand Up @@ -672,6 +674,11 @@ func (m *PeerManager) Accepted(peerID types.NodeID, peerOpts ...func(*peerInfo))
peer = m.newPeerInfo(peerID)
}

// reset this to avoid penalizing peers for their past transgressions
for _, addr := range peer.AddressInfo {
addr.DialFailures = 0
}

// If all connections slots are full, but we allow upgrades (and we checked
// above that we have upgrade capacity), then we can look for a lower-scored
// peer to replace and if found accept the connection anyway and evict it.
Expand Down Expand Up @@ -1323,15 +1330,23 @@ func (p *peerInfo) Score() PeerScore {
return PeerScorePersistent
}

if p.MutableScore <= 0 {
score := p.MutableScore

for _, addr := range p.AddressInfo {
// DialFailures is reset when dials succeed, so this
// is either the number of dial failures or 0.
score -= int64(addr.DialFailures)
}

if score <= 0 {
return 0
}

if p.MutableScore >= math.MaxUint8 {
if score >= math.MaxUint8 {
return PeerScore(math.MaxUint8)
}

return PeerScore(p.MutableScore)
return PeerScore(score)
}

// Validate validates the peer info.
Expand Down

0 comments on commit ef5886e

Please sign in to comment.