From e8dc887c6fd5d8e0a07f073ff8d210f0e3d4e4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Mon, 13 May 2024 15:07:08 -0400 Subject: [PATCH] fix(peer-manager): use backoff only on connection errors (#1089) --- waku/v2/peermanager/peer_connector.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/waku/v2/peermanager/peer_connector.go b/waku/v2/peermanager/peer_connector.go index 8dc8e603..6f40a171 100644 --- a/waku/v2/peermanager/peer_connector.go +++ b/waku/v2/peermanager/peer_connector.go @@ -193,7 +193,6 @@ func (c *PeerConnectionStrategy) canDialPeer(pi peer.AddrInfo) bool { c.mux.Lock() defer c.mux.Unlock() val, ok := c.cache.Get(pi.ID) - var cachedPeer *connCacheData if ok { tv := val.(*connCacheData) now := time.Now() @@ -204,15 +203,25 @@ func (c *PeerConnectionStrategy) canDialPeer(pi peer.AddrInfo) bool { } c.logger.Debug("Proceeding with connecting to peer", zap.Time("currentTime", now), zap.Time("nextTry", tv.nextTry)) - tv.nextTry = now.Add(tv.strat.Delay()) + } + return true +} + +func (c *PeerConnectionStrategy) addConnectionBackoff(peerID peer.ID) { + c.mux.Lock() + defer c.mux.Unlock() + val, ok := c.cache.Get(peerID) + var cachedPeer *connCacheData + if ok { + tv := val.(*connCacheData) + tv.nextTry = time.Now().Add(tv.strat.Delay()) } else { cachedPeer = &connCacheData{strat: c.backoff()} cachedPeer.nextTry = time.Now().Add(cachedPeer.strat.Delay()) c.logger.Debug("Initializing connectionCache for peer ", - logging.HostID("peerID", pi.ID), zap.Time("until", cachedPeer.nextTry)) - c.cache.Add(pi.ID, cachedPeer) + logging.HostID("peerID", peerID), zap.Time("until", cachedPeer.nextTry)) + c.cache.Add(peerID, cachedPeer) } - return true } func (c *PeerConnectionStrategy) dialPeers() { @@ -255,6 +264,7 @@ func (c *PeerConnectionStrategy) dialPeer(pi peer.AddrInfo, sem chan struct{}) { defer cancel() err := c.host.Connect(ctx, pi) if err != nil && !errors.Is(err, context.Canceled) { + c.addConnectionBackoff(pi.ID) c.host.Peerstore().(wps.WakuPeerstore).AddConnFailure(pi) c.logger.Warn("connecting to peer", logging.HostID("peerID", pi.ID), zap.Error(err)) }