diff options
author | Sonic <sonic@dexon.org> | 2019-04-02 19:00:34 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:59 +0800 |
commit | 66c5de46f2a18aae704b11059ee55c4ca3b21bd4 (patch) | |
tree | 66b550cf3c813ff278535b715076865400511a6f /p2p | |
parent | 37257d9e7981661d3f19eae5ea5d49bcedea3698 (diff) | |
download | dexon-66c5de46f2a18aae704b11059ee55c4ca3b21bd4.tar dexon-66c5de46f2a18aae704b11059ee55c4ca3b21bd4.tar.gz dexon-66c5de46f2a18aae704b11059ee55c4ca3b21bd4.tar.bz2 dexon-66c5de46f2a18aae704b11059ee55c4ca3b21bd4.tar.lz dexon-66c5de46f2a18aae704b11059ee55c4ca3b21bd4.tar.xz dexon-66c5de46f2a18aae704b11059ee55c4ca3b21bd4.tar.zst dexon-66c5de46f2a18aae704b11059ee55c4ca3b21bd4.zip |
p2p, p2p/discover: more aggressive dial strategy for direct dial (#326)
* p2p/discover: bump failure counter only if no nodes were provided
* p2p: more aggressive dial strategy for direct dial
Diffstat (limited to 'p2p')
-rw-r--r-- | p2p/dial.go | 34 | ||||
-rw-r--r-- | p2p/dial_test.go | 2 |
2 files changed, 25 insertions, 11 deletions
diff --git a/p2p/dial.go b/p2p/dial.go index cd226c53a..583f02f6b 100644 --- a/p2p/dial.go +++ b/p2p/dial.go @@ -33,6 +33,8 @@ const ( // redialing a certain node. dialHistoryExpiration = 30 * time.Second + directDialHistoryExpiration = 10 * time.Second + // Discovery lookups are throttled and can only run // once every few seconds. lookupInterval = 4 * time.Second @@ -223,7 +225,6 @@ func (s *dialstate) newTasks(nRunning int, peers map[enode.ID]*Peer, now time.Ti log.Warn("Removing direct dial candidate", "id", t.dest.ID(), "addr", &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}, "err", err) delete(s.direct, t.dest.ID()) case nil: - log.Debug("Direct peer connected", "id", id) s.dialing[id] = t.flags newtasks = append(newtasks, t) } @@ -306,7 +307,11 @@ func (s *dialstate) checkDial(n *enode.Node, peers map[enode.ID]*Peer) error { func (s *dialstate) taskDone(t task, now time.Time) { switch t := t.(type) { case *dialTask: - s.hist.add(t.dest.ID(), now.Add(dialHistoryExpiration)) + expiration := dialHistoryExpiration + if t.flags&directDialedConn != 0 { + expiration = directDialHistoryExpiration + } + s.hist.add(t.dest.ID(), now.Add(expiration)) delete(s.dialing, t.dest.ID()) case *discoverTask: s.lookupRunning = false @@ -322,11 +327,17 @@ func (t *dialTask) Do(srv *Server) { } err := t.dial(srv, t.dest) if err != nil { - log.Trace("Dial error", "task", t, "err", err) + if t.flags&directDialedConn != 0 { + log.Debug("Direct dial error", "task", t, "err", err) + } else { + log.Trace("Dial error", "task", t, "err", err) + } // Try resolving the ID of static nodes if dialing failed. if _, ok := err.(*dialError); ok && t.flags&(staticDialedConn|directDialedConn) != 0 { if t.resolve(srv) { - t.dial(srv, t.dest) + if err := t.dial(srv, t.dest); err != nil && t.flags&(directDialedConn) != 0 { + log.Debug("Direct dial error 2", "task", t, "err", err) + } } } } @@ -340,7 +351,7 @@ func (t *dialTask) Do(srv *Server) { // The backoff delay resets when the node is found. func (t *dialTask) resolve(srv *Server) bool { if srv.ntab == nil { - log.Debug("Can't resolve node", "id", t.dest.ID, "err", "discovery is disabled") + log.Debug("Can't resolve node", "id", t.dest.ID(), "err", "discovery is disabled") return false } if t.resolveDelay == 0 { @@ -352,17 +363,20 @@ func (t *dialTask) resolve(srv *Server) bool { resolved := srv.ntab.Resolve(t.dest) t.lastResolved = time.Now() if resolved == nil { - t.resolveDelay *= 2 - if t.resolveDelay > maxResolveDelay { - t.resolveDelay = maxResolveDelay + // Only backoff delay if this is not direct connection. + if t.flags&directDialedConn == 0 { + t.resolveDelay *= 2 + if t.resolveDelay > maxResolveDelay { + t.resolveDelay = maxResolveDelay + } } - log.Debug("Resolving node failed", "id", t.dest.ID, "newdelay", t.resolveDelay) + log.Debug("Resolving node failed", "id", t.dest.ID(), "newdelay", t.resolveDelay) return false } // The node was found. t.resolveDelay = initialResolveDelay t.dest = resolved - log.Debug("Resolved node", "id", t.dest.ID, "addr", &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}) + log.Debug("Resolved node", "id", t.dest.ID(), "addr", &net.TCPAddr{IP: t.dest.IP(), Port: t.dest.TCP()}) return true } diff --git a/p2p/dial_test.go b/p2p/dial_test.go index ab687c2ea..84f138899 100644 --- a/p2p/dial_test.go +++ b/p2p/dial_test.go @@ -581,7 +581,7 @@ func TestDialStateDirectDial(t *testing.T) { &dialTask{flags: directDialedConn, dest: newNode(uintID(5), nil)}, }, new: []task{ - &waitExpireTask{Duration: 14 * time.Second}, + &waitExpireTask{Duration: 10 * time.Second}, }, }, // Wait a round for dial history to expire, no new tasks should spawn. |