p2p: reduce ability of SendError to disconnect peers (#8597)

This commit is contained in:
Sam Kleinman
2022-05-24 11:19:32 -04:00
committed by GitHub
parent 1a52b7cb7b
commit d59a53be01
3 changed files with 22 additions and 2 deletions
+1
View File
@@ -46,6 +46,7 @@ type Wrapper interface {
type PeerError struct {
NodeID types.NodeID
Err error
Fatal bool
}
func (pe PeerError) Error() string { return fmt.Sprintf("peer=%q: %s", pe.NodeID, pe.Err.Error()) }
+7
View File
@@ -430,6 +430,13 @@ func (m *PeerManager) PeerRatio() float64 {
return float64(m.store.Size()) / float64(m.options.MaxPeers)
}
func (m *PeerManager) HasMaxPeerCapacity() bool {
m.mtx.Lock()
defer m.mtx.Unlock()
return len(m.connected) >= int(m.options.MaxConnected)
}
// DialNext finds an appropriate peer address to dial, and marks it as dialing.
// If no peer is found, or all connection slots are full, it blocks until one
// becomes available. The caller must call Dialed() or DialFailed() for the
+14 -2
View File
@@ -396,9 +396,21 @@ func (r *Router) routeChannel(
return
}
r.logger.Error("peer error, evicting", "peer", peerError.NodeID, "err", peerError.Err)
shouldEvict := peerError.Fatal || r.peerManager.HasMaxPeerCapacity()
r.logger.Error("peer error",
"peer", peerError.NodeID,
"err", peerError.Err,
"evicting", shouldEvict,
)
if shouldEvict {
r.peerManager.Errored(peerError.NodeID, peerError.Err)
} else {
r.peerManager.processPeerEvent(ctx, PeerUpdate{
NodeID: peerError.NodeID,
Status: PeerStatusBad,
})
}
r.peerManager.Errored(peerError.NodeID, peerError.Err)
case <-ctx.Done():
return
}