From 2e98706a997f09dff7d63d79ae860c256f56ba42 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Tue, 26 Nov 2019 11:14:43 +0100 Subject: [PATCH] p2p/discover: slow down lookups on empty table (#20389) * p2p/discover: slow down lookups on empty table * p2p/discover: wake from slowdown sleep when table is closed --- p2p/discover/lookup.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/p2p/discover/lookup.go b/p2p/discover/lookup.go index f988e0683..ab825fb05 100644 --- a/p2p/discover/lookup.go +++ b/p2p/discover/lookup.go @@ -18,6 +18,7 @@ package discover import ( "context" + "time" "github.com/ethereum/go-ethereum/p2p/enode" ) @@ -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 @@ -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)