aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSonic <sonic@dexon.org>2019-04-02 19:00:34 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 13:50:06 +0800
commit722d16e6ef1684dcd3df854e02bd89644a7fa290 (patch)
treec3feb05bd0e63fce67d7d0ac9861283e4a91e930
parenteda117879ae0b91d70f0b2abc1e2a959df43e8df (diff)
downloaddexon-722d16e6ef1684dcd3df854e02bd89644a7fa290.tar.gz
dexon-722d16e6ef1684dcd3df854e02bd89644a7fa290.tar.zst
dexon-722d16e6ef1684dcd3df854e02bd89644a7fa290.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
-rw-r--r--dex/nodetable.go2
-rw-r--r--p2p/dial.go34
-rw-r--r--p2p/dial_test.go2
3 files changed, 26 insertions, 12 deletions
diff --git a/dex/nodetable.go b/dex/nodetable.go
index ba1c28994..cc1de160f 100644
--- a/dex/nodetable.go
+++ b/dex/nodetable.go
@@ -42,7 +42,7 @@ func (t *nodeTable) AddRecords(records []*enr.Record) {
}
if n, ok := t.entry[node.ID()]; ok && n.Seq() >= node.Seq() {
- log.Debug("Ignore new record, already exists", "id", node.ID().String(),
+ log.Trace("Ignore new record, already exists", "id", node.ID().String(),
"ip", node.IP().String(), "udp", node.UDP(), "tcp", node.TCP())
continue
}
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.