From c1afb3c0cc9b1ffa0f1606460a344312a866c32d Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Sat, 20 Apr 2024 10:07:20 +0200 Subject: [PATCH] p2p/discover: return nextTime from run() --- p2p/discover/table.go | 52 ++++++++++++++++++------------------- p2p/discover/table_reval.go | 32 +++++++++++++---------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/p2p/discover/table.go b/p2p/discover/table.go index ecbf585d92..fbcf02019b 100644 --- a/p2p/discover/table.go +++ b/p2p/discover/table.go @@ -66,7 +66,7 @@ type Table struct { mutex sync.Mutex // protects buckets, bucket content, nursery, rand buckets [nBuckets]*bucket // index of known nodes by distance nursery []*node // bootstrap nodes - rand *reseedingRandom // source of randomness, periodically reseeded + rand reseedingRandom // source of randomness, periodically reseeded ips netutil.DistinctNetSet revalidation tableRevalidation @@ -76,14 +76,14 @@ type Table struct { log log.Logger // loop channels - refreshReq chan chan struct{} - revalidateResp chan revalidationResponse - addNodeCh chan addNodeRequest - addNodeHandled chan struct{} - findFailureCh chan *node - initDone chan struct{} - closeReq chan struct{} - closed chan struct{} + refreshReq chan chan struct{} + revalResponseCh chan revalidationResponse + addNodeCh chan addNodeRequest + addNodeHandled chan struct{} + findFailureCh chan *node + initDone chan struct{} + closeReq chan struct{} + closed chan struct{} nodeAddedHook func(*bucket, *node) nodeRemovedHook func(*bucket, *node) @@ -115,20 +115,19 @@ type addNodeRequest struct { func newTable(t transport, db *enode.DB, cfg Config) (*Table, error) { cfg = cfg.withDefaults() tab := &Table{ - net: t, - db: db, - cfg: cfg, - log: cfg.Log, - refreshReq: make(chan chan struct{}), - revalidateResp: make(chan revalidationResponse), - addNodeCh: make(chan addNodeRequest), - addNodeHandled: make(chan struct{}), - findFailureCh: make(chan *node), - initDone: make(chan struct{}), - closeReq: make(chan struct{}), - closed: make(chan struct{}), - rand: new(reseedingRandom), - ips: netutil.DistinctNetSet{Subnet: tableSubnet, Limit: tableIPLimit}, + net: t, + db: db, + cfg: cfg, + log: cfg.Log, + refreshReq: make(chan chan struct{}), + revalResponseCh: make(chan revalidationResponse), + addNodeCh: make(chan addNodeRequest), + addNodeHandled: make(chan struct{}), + findFailureCh: make(chan *node), + initDone: make(chan struct{}), + closeReq: make(chan struct{}), + closed: make(chan struct{}), + ips: netutil.DistinctNetSet{Subnet: tableSubnet, Limit: tableIPLimit}, } for i := range tab.buckets { tab.buckets[i] = &bucket{ @@ -349,17 +348,18 @@ func (tab *Table) loop() { loop: for { + nextTime := tab.revalidation.run(tab, tab.cfg.Clock.Now()) + revalTimer.Schedule(nextTime) + reseedRandTimer.Schedule(tab.rand.nextReseedTime()) - revalTimer.Schedule(tab.revalidation.nextTime()) select { case <-reseedRandTimer.C(): tab.rand.seed(tab.cfg.Clock.Now()) case <-revalTimer.C(): - tab.revalidation.run(tab, mclock.Now()) - case r := <-tab.revalidateResp: + case r := <-tab.revalResponseCh: tab.revalidation.handleResponse(tab, r) case addreq := <-tab.addNodeCh: diff --git a/p2p/discover/table_reval.go b/p2p/discover/table_reval.go index 93471d1608..7e5c96211c 100644 --- a/p2p/discover/table_reval.go +++ b/p2p/discover/table_reval.go @@ -50,7 +50,7 @@ func (tr *tableRevalidation) init(cfg *Config) { // nodeAdded is called when the table receives a new node. func (tr *tableRevalidation) nodeAdded(tab *Table, n *node) { - tr.newNodes.push(n, tab.cfg.Clock.Now(), tab.rand) + tr.newNodes.push(n, tab.cfg.Clock.Now(), &tab.rand) } // nodeRemoved is called when a node was removed from the table. @@ -61,22 +61,26 @@ func (tr *tableRevalidation) nodeRemoved(n *node) { } } -// nextTime returns the next time run() should be invoked. -// The Table main loop uses this to schedule a timer. -func (tr *tableRevalidation) nextTime() mclock.AbsTime { - return min(tr.newNodes.nextTime, tr.nodes.nextTime) -} - // run performs node revalidation. -func (tr *tableRevalidation) run(tab *Table, now mclock.AbsTime) { - if n := tr.newNodes.get(now, tab.rand, tr.activeReq); n != nil { +// It returns the next time it should be invoked, which is used in the Table main loop +// to schedule a timer. However, run can be called at any time. +func (tr *tableRevalidation) run(tab *Table, now mclock.AbsTime) (nextTime mclock.AbsTime) { + if n := tr.newNodes.get(now, &tab.rand, tr.activeReq); n != nil { tr.startRequest(tab, n, true) - tr.newNodes.schedule(now, tab.rand) + tr.newNodes.schedule(now, &tab.rand) } - if n := tr.nodes.get(now, tab.rand, tr.activeReq); n != nil { + if n := tr.nodes.get(now, &tab.rand, tr.activeReq); n != nil { tr.startRequest(tab, n, false) - tr.nodes.schedule(now, tab.rand) + tr.nodes.schedule(now, &tab.rand) } + + if tr.newNodes.nextTime == never { + return tr.nodes.nextTime + } + if tr.nodes.nextTime == never { + return tr.newNodes.nextTime + } + return min(tr.newNodes.nextTime, tr.nodes.nextTime) } // startRequest spawns a revalidation request for node n. @@ -111,7 +115,7 @@ func (tab *Table) doRevalidate(resp revalidationResponse, node *enode.Node) { } select { - case tab.revalidateResp <- resp: + case tab.revalResponseCh <- resp: case <-tab.closed: } } @@ -150,7 +154,7 @@ func (tr *tableRevalidation) handleResponse(tab *Table, resp revalidationRespons // Move node over to main queue after first validation. if resp.isNewNode { tr.newNodes.remove(n) - tr.nodes.push(n, tab.cfg.Clock.Now(), tab.rand) + tr.nodes.push(n, tab.cfg.Clock.Now(), &tab.rand) } // Store potential seeds in database.