svc/connect.in: Maintain time of last reconnect attempt and add rate limit.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 28 Apr 2022 14:16:46 +0000 (15:16 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 30 Apr 2022 00:51:38 +0000 (01:51 +0100)
This isn't used yet, but it's there as a general mechanism.

Don't rate-limit reconnections provoked by the pinger internally because
(a) these are relatively infrequent anyway by the nature of the pinger
mechanism, and (b) the current rate-limiting strategy is simply to
ignore a reconnection request if we've already made an attempt fairly
recently, so there isn't (and it's not worth building) the necessary
machinery to defer reconnection, and determine whether a deferred
reconnection is still needed.

svc/connect.in

index e679ff5..fa5a22c 100644 (file)
@@ -413,6 +413,7 @@ class PingPeer (object):
       me._ping()
     else:
       me._timer = M.SelTimer(now + me._every, me._time)
+    me._last_reconn = now
 
   def update(me, peer):
     """
@@ -451,11 +452,22 @@ class PingPeer (object):
         if me._connectp: T.spawn(run_connect, peer, peer.get('connect'))
         me._timer = M.SelTimer(now + me._every, me._time)
         me._sabotage = False
+        me._last_reconn = now
       else:
         S.kill(me._peer)
     except T.TripeError, e:
       if e.args[0] == 'unknown-peer': me._pinger.kill(me._peer)
 
+  def reconnect(me):
+    """
+    Attempt reconnection to the peer.
+
+    Applies rate-limiting so that we don't hammer a remote peer just because
+    we notice several problems in a short time interval.
+    """
+    now = time()
+    if now >= me._last_reconn + 5: me._reconnect(now)
+
   def event(me, code, stuff):
     """
     Respond to an event which happened to this peer.