Skip to content

Commit c6aa3fb

Browse files
committed
p2p/discover: remove hot-spin in table refresh trigger
1 parent bc0a21a commit c6aa3fb

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
@@ -153,6 +153,7 @@ type lookupIterator struct {
153153
cancel func()
154154
lookup *lookup
155155
tabRefreshing <-chan struct{}
156+
lastWait time.Time
156157
}
157158

158159
type lookupFunc func(ctx context.Context) *lookup
@@ -220,6 +221,21 @@ func (it *lookupIterator) lookupFailed(tab *Table, timeout time.Duration) {
220221
return
221222
}
222223

224+
// Ensure we trigger refresh at most every 2s.
225+
const minInterval = 2 * time.Second
226+
now := time.Now()
227+
diff := now.Sub(it.lastWait)
228+
it.lastWait = now
229+
if diff < minInterval {
230+
wait := time.NewTimer(diff)
231+
defer wait.Stop()
232+
select {
233+
case <-wait.C:
234+
case <-tout.Done():
235+
return
236+
}
237+
}
238+
223239
// Wait for ongoing refresh operation, or trigger one.
224240
if it.tabRefreshing == nil {
225241
it.tabRefreshing = tab.refresh()

0 commit comments

Comments
 (0)