From 568c0044a0a5671dc87b53c7f25d82bb1253d10a Mon Sep 17 00:00:00 2001 From: "Todd T. Fries" Date: Thu, 26 Sep 2013 16:26:31 -0500 Subject: [PATCH] introduce a backoff behavior for peers that are not permitting connections with help from davec o implement peer { retrycount int64 .. o count connect failures per peer o calculate backoff as 10s * retrycount / 2 --- peer.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/peer.go b/peer.go index 2407c452..f73aeecc 100644 --- a/peer.go +++ b/peer.go @@ -108,6 +108,7 @@ type peer struct { knownInventory *MruInventoryMap knownInvMutex sync.Mutex lastBlock int32 + retrycount int64 prevGetBlocksBegin *btcwire.ShaHash prevGetBlocksStop *btcwire.ShaHash prevGetBlockMutex sync.Mutex @@ -1208,6 +1209,8 @@ func newOutboundPeer(s *server, addr string, persistent bool) *peer { log.Debugf("[SRVR] Attempting to connect to %s", faddr) conn, err := dial("tcp", addr) if err != nil { + baseRetryInterval := time.Second * 10 + p.retrycount += 1 log.Errorf("[SRVR] Failed to connect to %s: %v", faddr, err) if !persistent { @@ -1215,9 +1218,11 @@ func newOutboundPeer(s *server, addr string, persistent bool) *peer { p.wg.Done() return } + scaledRetryInterval := baseRetryInterval.Nanoseconds() * p.retrycount / 2 + scaledRetrySeconds := time.Duration(scaledRetryInterval) log.Infof("[SRVR] Retrying connection to %s "+ - "in %s", faddr, connectionRetryInterval) - time.Sleep(connectionRetryInterval) + "in %v", faddr, scaledRetrySeconds) + time.Sleep(scaledRetrySeconds) continue }