Skip to content

Commit 2e98706

Browse files
fjlkaralabe
authored andcommitted
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
1 parent 8c1e8de commit 2e98706

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

p2p/discover/lookup.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package discover
1818

1919
import (
2020
"context"
21+
"time"
2122

2223
"github.com/ethereum/go-ethereum/p2p/enode"
2324
)
@@ -106,6 +107,12 @@ func (it *lookup) startQueries() bool {
106107
it.tab.mutex.Lock()
107108
closest := it.tab.closest(it.result.target, bucketSize, false)
108109
it.tab.mutex.Unlock()
110+
// Avoid finishing the lookup too quickly if table is empty. It'd be better to wait
111+
// for the table to fill in this case, but there is no good mechanism for that
112+
// yet.
113+
if len(closest.entries) == 0 {
114+
it.slowdown()
115+
}
109116
it.queries = 1
110117
it.replyCh <- closest.entries
111118
return true
@@ -124,6 +131,15 @@ func (it *lookup) startQueries() bool {
124131
return it.queries > 0
125132
}
126133

134+
func (it *lookup) slowdown() {
135+
sleep := time.NewTimer(1 * time.Second)
136+
defer sleep.Stop()
137+
select {
138+
case <-sleep.C:
139+
case <-it.tab.closeReq:
140+
}
141+
}
142+
127143
func (it *lookup) query(n *node, reply chan<- []*node) {
128144
fails := it.tab.db.FindFails(n.ID(), n.IP())
129145
r, err := it.queryfunc(n)

0 commit comments

Comments
 (0)