Skip to content

Commit 6fb810a

Browse files
committed
p2p: throttle all discovery lookups
Lookup calls would spin out of control when network connectivity was lost. The throttling that was in place only took effect when the table returned zero results, which doesn't happen very often. The new throttling should not have a negative impact when the host is online. Lookups against the network take some time and dials for all results must complete or hit the cache before a new one is started. This usually takes longer than four seconds, leaving online lookups unaffected. Fixes #1296
1 parent 3deded2 commit 6fb810a

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

p2p/dial.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ const (
1717
// redialing a certain node.
1818
dialHistoryExpiration = 30 * time.Second
1919

20-
// Discovery lookup tasks will wait for this long when
21-
// no results are returned. This can happen if the table
22-
// becomes empty (i.e. not often).
23-
emptyLookupDelay = 10 * time.Second
20+
// Discovery lookups are throttled and can only run
21+
// once every few seconds.
22+
lookupInterval = 4 * time.Second
2423
)
2524

2625
// dialstate schedules dials and discovery lookups.
@@ -206,18 +205,19 @@ func (t *dialTask) String() string {
206205
func (t *discoverTask) Do(srv *Server) {
207206
if t.bootstrap {
208207
srv.ntab.Bootstrap(srv.BootstrapNodes)
209-
} else {
210-
var target discover.NodeID
211-
rand.Read(target[:])
212-
t.results = srv.ntab.Lookup(target)
213-
// newTasks generates a lookup task whenever dynamic dials are
214-
// necessary. Lookups need to take some time, otherwise the
215-
// event loop spins too fast. An empty result can only be
216-
// returned if the table is empty.
217-
if len(t.results) == 0 {
218-
time.Sleep(emptyLookupDelay)
219-
}
208+
return
209+
}
210+
// newTasks generates a lookup task whenever dynamic dials are
211+
// necessary. Lookups need to take some time, otherwise the
212+
// event loop spins too fast.
213+
next := srv.lastLookup.Add(lookupInterval)
214+
if now := time.Now(); now.Before(next) {
215+
time.Sleep(next.Sub(now))
220216
}
217+
srv.lastLookup = time.Now()
218+
var target discover.NodeID
219+
rand.Read(target[:])
220+
t.results = srv.ntab.Lookup(target)
221221
}
222222

223223
func (t *discoverTask) String() (s string) {

p2p/server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type Server struct {
115115
ntab discoverTable
116116
listener net.Listener
117117
ourHandshake *protoHandshake
118+
lastLookup time.Time
118119

119120
// These are for Peers, PeerCount (and nothing else).
120121
peerOp chan peerOpFunc

0 commit comments

Comments
 (0)