aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeffrey Wilcke <jeffrey@ethereum.org>2015-06-23 20:36:58 +0800
committerJeffrey Wilcke <jeffrey@ethereum.org>2015-06-23 20:36:58 +0800
commit139439dcdc14e448bfdbd509bb135faa92337a28 (patch)
tree1d05199126268c927ae9acdd55fa088c0300ec19
parent9cf7913c61c6f9c1f36a44778a75e6a91a741be4 (diff)
parent6fb810adaa539a2fa97cf4481588b339ab5279ae (diff)
downloadgo-tangerine-139439dcdc14e448bfdbd509bb135faa92337a28.tar
go-tangerine-139439dcdc14e448bfdbd509bb135faa92337a28.tar.gz
go-tangerine-139439dcdc14e448bfdbd509bb135faa92337a28.tar.bz2
go-tangerine-139439dcdc14e448bfdbd509bb135faa92337a28.tar.lz
go-tangerine-139439dcdc14e448bfdbd509bb135faa92337a28.tar.xz
go-tangerine-139439dcdc14e448bfdbd509bb135faa92337a28.tar.zst
go-tangerine-139439dcdc14e448bfdbd509bb135faa92337a28.zip
Merge pull request #1309 from fjl/p2p-fix-lookup-spin
p2p: throttle all discovery lookups
-rw-r--r--p2p/dial.go30
-rw-r--r--p2p/server.go1
2 files changed, 16 insertions, 15 deletions
diff --git a/p2p/dial.go b/p2p/dial.go
index 71065c5ee..b82d6d1f5 100644
--- a/p2p/dial.go
+++ b/p2p/dial.go
@@ -17,10 +17,9 @@ const (
// redialing a certain node.
dialHistoryExpiration = 30 * time.Second
- // Discovery lookup tasks will wait for this long when
- // no results are returned. This can happen if the table
- // becomes empty (i.e. not often).
- emptyLookupDelay = 10 * time.Second
+ // Discovery lookups are throttled and can only run
+ // once every few seconds.
+ lookupInterval = 4 * time.Second
)
// dialstate schedules dials and discovery lookups.
@@ -206,18 +205,19 @@ func (t *dialTask) String() string {
func (t *discoverTask) Do(srv *Server) {
if t.bootstrap {
srv.ntab.Bootstrap(srv.BootstrapNodes)
- } else {
- var target discover.NodeID
- rand.Read(target[:])
- t.results = srv.ntab.Lookup(target)
- // newTasks generates a lookup task whenever dynamic dials are
- // necessary. Lookups need to take some time, otherwise the
- // event loop spins too fast. An empty result can only be
- // returned if the table is empty.
- if len(t.results) == 0 {
- time.Sleep(emptyLookupDelay)
- }
+ return
+ }
+ // newTasks generates a lookup task whenever dynamic dials are
+ // necessary. Lookups need to take some time, otherwise the
+ // event loop spins too fast.
+ next := srv.lastLookup.Add(lookupInterval)
+ if now := time.Now(); now.Before(next) {
+ time.Sleep(next.Sub(now))
}
+ srv.lastLookup = time.Now()
+ var target discover.NodeID
+ rand.Read(target[:])
+ t.results = srv.ntab.Lookup(target)
}
func (t *discoverTask) String() (s string) {
diff --git a/p2p/server.go b/p2p/server.go
index 59b97a0aa..5eff70345 100644
--- a/p2p/server.go
+++ b/p2p/server.go
@@ -115,6 +115,7 @@ type Server struct {
ntab discoverTable
listener net.Listener
ourHandshake *protoHandshake
+ lastLookup time.Time
// These are for Peers, PeerCount (and nothing else).
peerOp chan peerOpFunc