Browse Source

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.
pull/7283/head
Sam Kleinman 3 years ago
committed by GitHub
parent
commit
27560cf7a4
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 3 deletions
  1. +1
    -0
      CHANGELOG_PENDING.md
  2. +18
    -3
      internal/p2p/peermanager.go

+ 1
- 0
CHANGELOG_PENDING.md View File

@ -23,6 +23,7 @@ Special thanks to external contributors on this release:
- P2P Protocol - P2P Protocol
- [p2p] \#7035 Remove legacy P2P routing implementation and associated configuration options. (@tychoish) - [p2p] \#7035 Remove legacy P2P routing implementation and associated configuration options. (@tychoish)
- [p2p] \#7265 Peer manager reduces peer score for each failed dial attempts for peers that have not successfully dialed. (@tychoish)
- Go API - Go API


+ 18
- 3
internal/p2p/peermanager.go View File

@ -532,6 +532,7 @@ func (m *PeerManager) DialFailed(address NodeAddress) error {
if !ok { if !ok {
return nil // Assume the address has been removed, ignore. return nil // Assume the address has been removed, ignore.
} }
addressInfo.LastDialFailure = time.Now().UTC() addressInfo.LastDialFailure = time.Now().UTC()
addressInfo.DialFailures++ addressInfo.DialFailures++
if err := m.store.Set(peer); err != nil { if err := m.store.Set(peer); err != nil {
@ -602,6 +603,7 @@ func (m *PeerManager) Dialed(address NodeAddress) error {
addressInfo.LastDialSuccess = now addressInfo.LastDialSuccess = now
// If not found, assume address has been removed. // If not found, assume address has been removed.
} }
if err := m.store.Set(peer); err != nil { if err := m.store.Set(peer); err != nil {
return err return err
} }
@ -660,6 +662,11 @@ func (m *PeerManager) Accepted(peerID types.NodeID) error {
peer = m.newPeerInfo(peerID) 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 // 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 // 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. // peer to replace and if found accept the connection anyway and evict it.
@ -1287,15 +1294,23 @@ func (p *peerInfo) Score() PeerScore {
return PeerScorePersistent 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 return 0
} }
if p.MutableScore >= math.MaxUint8 {
if score >= math.MaxUint8 {
return PeerScore(math.MaxUint8) return PeerScore(math.MaxUint8)
} }
return PeerScore(p.MutableScore)
return PeerScore(score)
} }
// Validate validates the peer info. // Validate validates the peer info.


Loading…
Cancel
Save