From 7640fc1c0de98718bca068f6b7c3399d5c61ace7 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 25 Mar 2019 13:01:24 -0700 Subject: [PATCH] graceful backoffs on connection retries --- protocol/connections/connectionsmanager.go | 11 ++++++++--- protocol/connections/peerserverconnection.go | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/protocol/connections/connectionsmanager.go b/protocol/connections/connectionsmanager.go index 0f299d6..8c8b9e2 100644 --- a/protocol/connections/connectionsmanager.go +++ b/protocol/connections/connectionsmanager.go @@ -99,7 +99,9 @@ func (m *Manager) GetPeerServerConnectionForOnion(host string) (psc *PeerServerC // AttemptReconnections repeatedly attempts to reconnect with failed peers and servers. func (m *Manager) AttemptReconnections() { - timeout := time.Duration(0) // first pass right away + maxTimeout := time.Minute * 5 + // nearly instant first run, next few runs will prolly be too quick to have any FAILED and will gracefully slow to MAX after that + timeout := time.Millisecond * 500 for { select { @@ -120,8 +122,11 @@ func (m *Manager) AttemptReconnections() { } m.lock.Unlock() - // Launch Another Run In 30 Seconds - timeout = time.Duration(30 * time.Second) + if timeout < maxTimeout { + timeout = timeout * 2 + } else { + timeout = maxTimeout + } case <-m.breakChannel: return } diff --git a/protocol/connections/peerserverconnection.go b/protocol/connections/peerserverconnection.go index c567e20..2900ecb 100644 --- a/protocol/connections/peerserverconnection.go +++ b/protocol/connections/peerserverconnection.go @@ -102,7 +102,7 @@ func (psc *PeerServerConnection) SendGroupMessage(gm *protocol.GroupMessage) err errCount := 0 for errCount < 5 { - time.Sleep(time.Second * 1) + time.Sleep(time.Second * time.Duration(errCount+1)) // back off retry err = psc.connection.Do(func() error { channel := psc.connection.Channel("im.cwtch.server.send", channels.Outbound) if channel == nil {