eth/dropper: simplify cfg

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-04-10 17:11:01 +02:00
parent e9065acdde
commit 5da26a98d0
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E

View file

@ -50,7 +50,7 @@ const (
// randomly every peerDropInterval to make space for new peers // randomly every peerDropInterval to make space for new peers
// - peers are dropped separately from the inboud pool and from the dialed pool // - peers are dropped separately from the inboud pool and from the dialed pool
type dropper struct { type dropper struct {
dropperConfig cfg dropperConfig
peersFunc getPeersFunc peersFunc getPeersFunc
syncingFunc getSyncingFunc syncingFunc getSyncingFunc
@ -76,14 +76,10 @@ type getSyncingFunc func() bool
type dropperConfig struct { type dropperConfig struct {
maxDialPeers int // maximum number of dialed peers maxDialPeers int // maximum number of dialed peers
maxInboundPeers int // maximum number of inbound peers maxInboundPeers int // maximum number of inbound peers
log log.Logger
clock mclock.Clock clock mclock.Clock
} }
func (cfg dropperConfig) withDefaults() dropperConfig { func (cfg dropperConfig) withDefaults() dropperConfig {
if cfg.log == nil {
cfg.log = log.Root()
}
if cfg.clock == nil { if cfg.clock == nil {
cfg.clock = mclock.System{} cfg.clock = mclock.System{}
} }
@ -93,7 +89,7 @@ func (cfg dropperConfig) withDefaults() dropperConfig {
func newDropper(config *dropperConfig) *dropper { func newDropper(config *dropperConfig) *dropper {
cfg := config.withDefaults() cfg := config.withDefaults()
cm := &dropper{ cm := &dropper{
dropperConfig: cfg, cfg: cfg,
peerDropDialedTimer: mclock.NewAlarm(cfg.clock), peerDropDialedTimer: mclock.NewAlarm(cfg.clock),
peerDropInboundTimer: mclock.NewAlarm(cfg.clock), peerDropInboundTimer: mclock.NewAlarm(cfg.clock),
peerEventCh: make(chan *p2p.PeerEvent), peerEventCh: make(chan *p2p.PeerEvent),
@ -102,7 +98,7 @@ func newDropper(config *dropperConfig) *dropper {
if peerDropIntervalMin > peerDropIntervalMax { if peerDropIntervalMin > peerDropIntervalMax {
panic("peerDropIntervalMin duration must be less than or equal to peerDropIntervalMax duration") panic("peerDropIntervalMin duration must be less than or equal to peerDropIntervalMax duration")
} }
cm.log.Info("New Dropper", "maxDialPeers", cm.maxDialPeers, "threshold", peerDropThreshold, log.Info("New Dropper", "maxDialPeers", cm.cfg.maxDialPeers, "threshold", peerDropThreshold,
"intervalMin", peerDropIntervalMin, "intervalMax", peerDropIntervalMax) "intervalMin", peerDropIntervalMin, "intervalMax", peerDropIntervalMax)
return cm return cm
} }
@ -154,7 +150,7 @@ func (cm *dropper) dropRandomPeer(dialed bool) bool {
droppable := slices.DeleteFunc(peers, selectDoNotDrop) droppable := slices.DeleteFunc(peers, selectDoNotDrop)
if len(droppable) > 0 { if len(droppable) > 0 {
p := droppable[mrand.Intn(len(droppable))] p := droppable[mrand.Intn(len(droppable))]
cm.log.Debug("dropping random peer", "id", p.ID(), "duration", common.PrettyDuration(p.Lifetime()), log.Debug("dropping random peer", "id", p.ID(), "duration", common.PrettyDuration(p.Lifetime()),
"dialed", dialed, "peercountbefore", len(peers)) "dialed", dialed, "peercountbefore", len(peers))
p.Disconnect(p2p.DiscTooManyPeers) p.Disconnect(p2p.DiscTooManyPeers)
return true return true
@ -173,22 +169,22 @@ func randomDuration(min, max time.Duration) time.Duration {
// updatePeerDropTimers checks and starts/stops the timer for peer drop. // updatePeerDropTimers checks and starts/stops the timer for peer drop.
func (cm *dropper) updatePeerDropTimers(syncing bool) { func (cm *dropper) updatePeerDropTimers(syncing bool) {
numPeers, numDialed, numInbound := cm.numPeers() numPeers, numDialed, numInbound := cm.numPeers()
cm.log.Trace("Dropper status", "syncing", syncing, log.Trace("Dropper status", "syncing", syncing,
"peers", numPeers, "out", numDialed, "in", numInbound, "peers", numPeers, "out", numDialed, "in", numInbound,
"maxout", cm.maxDialPeers, "maxin", cm.maxInboundPeers) "maxout", cm.cfg.maxDialPeers, "maxin", cm.cfg.maxInboundPeers)
if !syncing { if !syncing {
// If a drop was already scheduled, Schedule does nothing. // If a drop was already scheduled, Schedule does nothing.
if cm.maxDialPeers-numDialed <= peerDropThreshold { if cm.cfg.maxDialPeers-numDialed <= peerDropThreshold {
interval := randomDuration(peerDropIntervalMin, peerDropIntervalMax) interval := randomDuration(peerDropIntervalMin, peerDropIntervalMax)
cm.peerDropDialedTimer.Schedule(cm.clock.Now().Add(interval)) cm.peerDropDialedTimer.Schedule(cm.cfg.clock.Now().Add(interval))
} else { } else {
cm.peerDropDialedTimer.Stop() cm.peerDropDialedTimer.Stop()
} }
if cm.maxInboundPeers-numInbound <= peerDropThreshold { if cm.cfg.maxInboundPeers-numInbound <= peerDropThreshold {
interval := randomDuration(peerDropIntervalMin, peerDropIntervalMax) interval := randomDuration(peerDropIntervalMin, peerDropIntervalMax)
cm.peerDropInboundTimer.Schedule(cm.clock.Now().Add(interval)) cm.peerDropInboundTimer.Schedule(cm.cfg.clock.Now().Add(interval))
} else { } else {
cm.peerDropInboundTimer.Stop() cm.peerDropInboundTimer.Stop()
} }
@ -209,9 +205,9 @@ func (cm *dropper) loop() {
// - subscribe to Downloader.mux // - subscribe to Downloader.mux
// - subscribe to DownloaderAPI (which itself polls the sync status) // - subscribe to DownloaderAPI (which itself polls the sync status)
syncing := cm.syncingFunc() syncing := cm.syncingFunc()
cm.log.Trace("Sync status", "syncing", syncing) log.Trace("Sync status", "syncing", syncing)
syncCheckTimer := mclock.NewAlarm(cm.dropperConfig.clock) syncCheckTimer := mclock.NewAlarm(cm.cfg.clock)
syncCheckTimer.Schedule(cm.clock.Now().Add(syncCheckInterval)) syncCheckTimer.Schedule(cm.cfg.clock.Now().Add(syncCheckInterval))
defer syncCheckTimer.Stop() defer syncCheckTimer.Stop()
for { for {
@ -221,11 +217,11 @@ func (cm *dropper) loop() {
syncingNew := cm.syncingFunc() syncingNew := cm.syncingFunc()
if syncing != syncingNew { if syncing != syncingNew {
// Syncing status changed, we might need to update the timers. // Syncing status changed, we might need to update the timers.
cm.log.Trace("Sync status changed", "syncing", syncingNew) log.Trace("Sync status changed", "syncing", syncingNew)
syncing = syncingNew syncing = syncingNew
cm.updatePeerDropTimers(syncing) cm.updatePeerDropTimers(syncing)
} }
syncCheckTimer.Schedule(cm.clock.Now().Add(syncCheckInterval)) syncCheckTimer.Schedule(cm.cfg.clock.Now().Add(syncCheckInterval))
case ev := <-cm.peerEventCh: case ev := <-cm.peerEventCh:
if ev.Type == p2p.PeerEventTypeAdd || ev.Type == p2p.PeerEventTypeDrop { if ev.Type == p2p.PeerEventTypeAdd || ev.Type == p2p.PeerEventTypeDrop {
// Number of peers changed, we might need to start the timers. // Number of peers changed, we might need to start the timers.