-
Notifications
You must be signed in to change notification settings - Fork 20.9k
eth: add logic to drop peers randomly when saturated #31476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
136d32d
p2p/connmanager: add connection manager to create some churn
cskiraly 7a49bf0
p2p/connmanager: only drop from dialed peers
cskiraly 91c1c30
p2p/connmanager: avoid dropping trusted peers
cskiraly 75e4c26
p2p/connmanager: avoid dropping peers too early
cskiraly 4635dac
p2p/connmanager: set meaningful defaults
cskiraly 23cda63
p2p/peer: expose conn flags through getter functions
cskiraly ea8d05a
p2p/server: expose MaxInboundConns and MaxDialedConns
cskiraly e0b0189
eth/connmanager: move Connection Manager to package eth
cskiraly c41569d
eth/connmanager: use slices.DeleteFunc to filter in place
cskiraly 61b26a9
eth/connman: fixup log levels
cskiraly 628c5e5
eth/connmanager: get sync status
cskiraly cb5d672
eth/connmanager: no need to store srv
cskiraly 301b396
eth/connmanager: monitor sync status
cskiraly 77d634c
eth/connmanager: handle inbound and dialed peers separately
cskiraly d46ef40
fixing newlines
cskiraly 8bb7f1e
eth/connmanager: randomize peer drop timers
cskiraly 42d2c9b
eth: renaming Connection Manager to Dropper
cskiraly e9065ac
simplify rand usage
cskiraly 5da26a9
eth/dropper: simplify cfg
cskiraly 75c8ee1
eth/dropper: simplify code
cskiraly 1647f51
eth/dropper: add metrics
cskiraly 7a76bdd
eth/dropper: simplify sync status query
cskiraly 4a69bf9
eth/dropper: fixing logs
cskiraly ff66b1c
eth/dropper: remove unused peerEvent channel
cskiraly 2a9372e
eth/dropper: changing error code to DiscUselessPeer
cskiraly 976e039
set doNotDropBefore to 10 minutes
cskiraly 4961445
Update dropper.go
fjl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
// Copyright 2015 The go-ethereum Authors | ||
// This file is part of the go-ethereum library. | ||
// | ||
// The go-ethereum library is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Lesser General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// The go-ethereum library is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Lesser General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Lesser General Public License | ||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package eth | ||
|
||
import ( | ||
crand "crypto/rand" | ||
"encoding/binary" | ||
mrand "math/rand" | ||
"slices" | ||
"sync" | ||
"time" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/common/mclock" | ||
"github.com/ethereum/go-ethereum/event" | ||
"github.com/ethereum/go-ethereum/log" | ||
"github.com/ethereum/go-ethereum/p2p" | ||
) | ||
|
||
const ( | ||
// Interval between peer drop events (uniform between min and max) | ||
peerDropIntervalMin = 3 * time.Minute | ||
// Interval between peer drop events (uniform between min and max) | ||
peerDropIntervalMax = 7 * time.Minute | ||
// Avoid dropping peers for some time after connection | ||
doNotDropBefore = 2 * peerDropIntervalMax | ||
// How close to max should we initiate the drop timer. O should be fine, | ||
// dropping when no more peers can be added. Larger numbers result in more | ||
// aggressive drop behavior. | ||
peerDropThreshold = 0 | ||
// Sync status poll interval (no need to be too reactive here) | ||
syncCheckInterval = 60 * time.Second | ||
) | ||
|
||
// connManager monitors the state of the peer pool and makes changes as follows: | ||
// - during sync the Downloader handles peer connections co connManager is disabled | ||
// - if not syncing and the peer count is close to the limit, it drops peers | ||
// randomly every peerDropInterval to make space for new peers | ||
// - peers are dropped separately from the inboud pool and from the dialed pool | ||
type connManager struct { | ||
connmanConfig | ||
peersFunc getPeersFunc | ||
syncingFunc getSyncingFunc | ||
|
||
// The peerDrop timers introduce churn if we are close to limit capacity. | ||
// We handle Dialed and Inbound connections separately | ||
peerDropDialedTimer *mclock.Alarm | ||
peerDropInboundTimer *mclock.Alarm | ||
|
||
peerEventCh chan *p2p.PeerEvent // channel for peer event changes | ||
sub event.Subscription // subscription to peerEventCh | ||
|
||
wg sync.WaitGroup // wg for graceful shutdown | ||
shutdownCh chan struct{} | ||
} | ||
|
||
// Callback type to get the list of connected peers. | ||
type getPeersFunc func() []*p2p.Peer | ||
|
||
// Callback type to get syncing status. | ||
// Returns true while syncing, false when synced. | ||
type getSyncingFunc func() bool | ||
|
||
type connmanConfig struct { | ||
maxDialPeers int // maximum number of dialed peers | ||
maxInboundPeers int // maximum number of inbound peers | ||
log log.Logger | ||
clock mclock.Clock | ||
rand *mrand.Rand | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func (cfg connmanConfig) withDefaults() connmanConfig { | ||
if cfg.log == nil { | ||
cfg.log = log.Root() | ||
} | ||
if cfg.clock == nil { | ||
cfg.clock = mclock.System{} | ||
} | ||
if cfg.rand == nil { | ||
seedb := make([]byte, 8) | ||
crand.Read(seedb) | ||
seed := int64(binary.BigEndian.Uint64(seedb)) | ||
cfg.rand = mrand.New(mrand.NewSource(seed)) | ||
} | ||
return cfg | ||
} | ||
|
||
func newConnManager(config *connmanConfig) *connManager { | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
cfg := config.withDefaults() | ||
cm := &connManager{ | ||
connmanConfig: cfg, | ||
peerDropDialedTimer: mclock.NewAlarm(cfg.clock), | ||
peerDropInboundTimer: mclock.NewAlarm(cfg.clock), | ||
peerEventCh: make(chan *p2p.PeerEvent), | ||
shutdownCh: make(chan struct{}), | ||
} | ||
if peerDropIntervalMin > peerDropIntervalMax { | ||
panic("peerDropIntervalMin duration must be less than or equal to peerDropIntervalMax duration") | ||
} | ||
cm.log.Info("New Connection Manager", "maxDialPeers", cm.maxDialPeers, "threshold", peerDropThreshold, | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"intervalMin", peerDropIntervalMin, "intervalMax", peerDropIntervalMax) | ||
return cm | ||
} | ||
|
||
// Start the connection manager. | ||
func (cm *connManager) Start(srv *p2p.Server, syncingFunc getSyncingFunc) { | ||
cm.wg.Add(1) | ||
cm.peersFunc = srv.Peers | ||
cm.syncingFunc = syncingFunc | ||
cm.sub = srv.SubscribeEvents(cm.peerEventCh) | ||
go cm.loop() | ||
} | ||
|
||
// Stop the connection manager. | ||
func (cm *connManager) Stop() { | ||
cm.sub.Unsubscribe() | ||
cm.peerDropInboundTimer.Stop() | ||
cm.peerDropDialedTimer.Stop() | ||
close(cm.shutdownCh) | ||
cm.wg.Wait() | ||
} | ||
|
||
// numPeers returns the current number of peers and its breakdown (dialed or inbound). | ||
func (cm *connManager) numPeers() (numPeers int, numDialed int, numInbound int) { | ||
peers := cm.peersFunc() | ||
dialed := slices.DeleteFunc(peers, (*p2p.Peer).Inbound) | ||
return len(peers), len(dialed), len(peers) - len(dialed) | ||
} | ||
|
||
// dropRandomPeer selects one of the peers randomly and drops it from the peer pool. | ||
func (cm *connManager) dropRandomPeer(dialed bool) bool { | ||
peers := cm.peersFunc() | ||
|
||
selectDoNotDrop := func(p *p2p.Peer) bool { | ||
if dialed { | ||
// Only drop from dyndialed peers. Avoid dropping trusted peers. | ||
// Give some time to peers before considering them for a drop. | ||
return !p.DynDialed() || | ||
p.Trusted() || | ||
p.Lifetime() < mclock.AbsTime(doNotDropBefore) | ||
} else { | ||
// Only drop from inbound peers. Avoid dropping trusted peers. | ||
// Give some time to peers before considering them for a drop. | ||
return p.DynDialed() || p.StaticDialed() || | ||
p.Trusted() || | ||
p.Lifetime() < mclock.AbsTime(doNotDropBefore) | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
droppable := slices.DeleteFunc(peers, selectDoNotDrop) | ||
if len(droppable) > 0 { | ||
p := droppable[cm.rand.Intn(len(droppable))] | ||
cm.log.Debug("dropping random peer", "id", p.ID(), "duration", common.PrettyDuration(p.Lifetime()), | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"dialed", dialed, "peercountbefore", len(peers)) | ||
p.Disconnect(p2p.DiscTooManyPeers) | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// randomDuration generates a random duration between min and max. | ||
// TODO: maybe we should move this to a common utlity package. | ||
// TODO: panic might be too harsh, maybe return an error. | ||
func randomDuration(rand *mrand.Rand, min, max time.Duration) time.Duration { | ||
if min > max { | ||
panic("min duration must be less than or equal to max duration") | ||
} | ||
nanos := rand.Int63n(max.Nanoseconds()-min.Nanoseconds()) + min.Nanoseconds() | ||
return time.Duration(nanos) | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// updatePeerDropTimers checks and starts/stops the timer for peer drop. | ||
func (cm *connManager) updatePeerDropTimers(syncing bool) { | ||
numPeers, numDialed, numInbound := cm.numPeers() | ||
cm.log.Trace("ConnManager status", "syncing", syncing, | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"peers", numPeers, "out", numDialed, "in", numInbound, | ||
"maxout", cm.maxDialPeers, "maxin", cm.maxInboundPeers) | ||
|
||
if !syncing { | ||
// If a drop was already scheduled, Schedule does nothing. | ||
if cm.maxDialPeers-numDialed <= peerDropThreshold { | ||
interval := randomDuration(cm.rand, peerDropIntervalMin, peerDropIntervalMax) | ||
cm.peerDropDialedTimer.Schedule(cm.clock.Now().Add(interval)) | ||
} else { | ||
cm.peerDropDialedTimer.Stop() | ||
} | ||
|
||
if cm.maxInboundPeers-numInbound <= peerDropThreshold { | ||
interval := randomDuration(cm.rand, peerDropIntervalMin, peerDropIntervalMax) | ||
cm.peerDropInboundTimer.Schedule(cm.clock.Now().Add(interval)) | ||
} else { | ||
cm.peerDropInboundTimer.Stop() | ||
} | ||
} else { | ||
// Downloader is managing connections while syncing. | ||
cm.peerDropDialedTimer.Stop() | ||
cm.peerDropInboundTimer.Stop() | ||
} | ||
} | ||
|
||
// loop is the main loop of the connection manager. | ||
func (cm *connManager) loop() { | ||
defer cm.wg.Done() | ||
|
||
// Set up periodic timer to pull syncing status. | ||
// We could get syncing status in a few ways: | ||
// - poll the sync status (we use this for now) | ||
// - subscribe to Downloader.mux | ||
// - subscribe to DownloaderAPI (which itself polls the sync status) | ||
syncing := cm.syncingFunc() | ||
cm.log.Trace("Sync status", "syncing", syncing) | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
syncCheckTimer := mclock.NewAlarm(cm.connmanConfig.clock) | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
syncCheckTimer.Schedule(cm.clock.Now().Add(syncCheckInterval)) | ||
defer syncCheckTimer.Stop() | ||
|
||
for { | ||
select { | ||
case <-syncCheckTimer.C(): | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Update info about syncing status, and rearm the timers. | ||
syncingNew := cm.syncingFunc() | ||
if syncing != syncingNew { | ||
// Syncing status changed, we might need to update the timers. | ||
cm.log.Trace("Sync status changed", "syncing", syncingNew) | ||
cskiraly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
syncing = syncingNew | ||
cm.updatePeerDropTimers(syncing) | ||
} | ||
syncCheckTimer.Schedule(cm.clock.Now().Add(syncCheckInterval)) | ||
case ev := <-cm.peerEventCh: | ||
if ev.Type == p2p.PeerEventTypeAdd || ev.Type == p2p.PeerEventTypeDrop { | ||
// Number of peers changed, we might need to start the timers. | ||
cm.updatePeerDropTimers(syncing) | ||
} | ||
case <-cm.peerDropDialedTimer.C(): | ||
cm.dropRandomPeer(true) | ||
case <-cm.peerDropInboundTimer.C(): | ||
cm.dropRandomPeer(false) | ||
case <-cm.shutdownCh: | ||
return | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.