Skip to content

Commit

Permalink
p2p/discover: slow down lookups on empty table (#20389)
Browse files Browse the repository at this point in the history
* p2p/discover: slow down lookups on empty table

* p2p/discover: wake from slowdown sleep when table is closed
  • Loading branch information
fjl authored and karalabe committed Nov 26, 2019
1 parent 8c1e8de commit 2e98706
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions p2p/discover/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package discover

import (
"context"
"time"

"github.com/ethereum/go-ethereum/p2p/enode"
)
Expand Down Expand Up @@ -106,6 +107,12 @@ func (it *lookup) startQueries() bool {
it.tab.mutex.Lock()
closest := it.tab.closest(it.result.target, bucketSize, false)
it.tab.mutex.Unlock()
// Avoid finishing the lookup too quickly if table is empty. It'd be better to wait
// for the table to fill in this case, but there is no good mechanism for that
// yet.
if len(closest.entries) == 0 {
it.slowdown()
}
it.queries = 1
it.replyCh <- closest.entries
return true
Expand All @@ -124,6 +131,15 @@ func (it *lookup) startQueries() bool {
return it.queries > 0
}

func (it *lookup) slowdown() {
sleep := time.NewTimer(1 * time.Second)
defer sleep.Stop()
select {
case <-sleep.C:
case <-it.tab.closeReq:
}
}

func (it *lookup) query(n *node, reply chan<- []*node) {
fails := it.tab.db.FindFails(n.ID(), n.IP())
r, err := it.queryfunc(n)
Expand Down

0 comments on commit 2e98706

Please sign in to comment.