mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-23 05:06:43 +00:00
p2p/discover: remove hot-spin on lookup creation
This commit is contained in:
parent
bc0a21a1d5
commit
3868d86670
1 changed files with 23 additions and 0 deletions
|
|
@ -153,6 +153,7 @@ type lookupIterator struct {
|
||||||
cancel func()
|
cancel func()
|
||||||
lookup *lookup
|
lookup *lookup
|
||||||
tabRefreshing <-chan struct{}
|
tabRefreshing <-chan struct{}
|
||||||
|
lastLookup time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
type lookupFunc func(ctx context.Context) *lookup
|
type lookupFunc func(ctx context.Context) *lookup
|
||||||
|
|
@ -185,6 +186,9 @@ func (it *lookupIterator) Next() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if it.lookup == nil {
|
if it.lookup == nil {
|
||||||
|
// Ensure enough time has passed between lookup creations.
|
||||||
|
it.slowdown()
|
||||||
|
|
||||||
it.lookup = it.nextLookup(it.ctx)
|
it.lookup = it.nextLookup(it.ctx)
|
||||||
if it.lookup.empty() {
|
if it.lookup.empty() {
|
||||||
// If the lookup is empty right after creation, it means the local table
|
// If the lookup is empty right after creation, it means the local table
|
||||||
|
|
@ -235,6 +239,25 @@ func (it *lookupIterator) lookupFailed(tab *Table, timeout time.Duration) {
|
||||||
tab.waitForNodes(tout, 1)
|
tab.waitForNodes(tout, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// slowdown applies a delay between creating lookups. This exists to prevent hot-spinning
|
||||||
|
// in some test environments where lookups don't yield any results.
|
||||||
|
func (it *lookupIterator) slowdown() {
|
||||||
|
const minInterval = 1 * time.Second
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
diff := now.Sub(it.lastLookup)
|
||||||
|
it.lastLookup = now
|
||||||
|
if diff > minInterval {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
wait := time.NewTimer(diff)
|
||||||
|
defer wait.Stop()
|
||||||
|
select {
|
||||||
|
case <-wait.C:
|
||||||
|
case <-it.ctx.Done():
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Close ends the iterator.
|
// Close ends the iterator.
|
||||||
func (it *lookupIterator) Close() {
|
func (it *lookupIterator) Close() {
|
||||||
it.cancel()
|
it.cancel()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue