Expire disconnected connections if they aren't needed or can't reconnect
authorAlexander van Gessel (AI0867) <ai0867@gmail.com>
Mon, 8 Oct 2012 10:29:49 +0000 (12:29 +0200)
committerAlexander van Gessel (AI0867) <ai0867@gmail.com>
Mon, 8 Oct 2012 10:37:04 +0000 (12:37 +0200)
irkerd

diff --git a/irkerd b/irkerd
index 302b48a0efec29ab76ae29c6b3562cf43a19d44d..6d6c15e431da1309f7abe286e28b36c97ade3b85 100755 (executable)
--- a/irkerd
+++ b/irkerd
@@ -187,7 +187,11 @@ class Connection:
                     now = time.time()
                     xmit_timeout = now > self.last_xmit + XMIT_TTL
                     ping_timeout = now > self.last_ping + PING_TTL
-                    if (xmit_timeout or ping_timeout) and self.status != "disconnected":
+                    if self.status == "disconnected":
+                        # If the queue is empty, we can drop this connection.
+                        self.status = "expired"
+                        break
+                    elif xmit_timeout or ping_timeout:
                         self.irker.debug(1, "timing out connection to %s at %s (ping_timeout=%s, xmit_timeout=%s)" % (self.servername, time.asctime(), ping_timeout, xmit_timeout))
                         with self.irker.library_lock:
                             self.connection.context = None
@@ -201,6 +205,13 @@ class Connection:
                         # reflex arc it is highly unlikely any human will ever
                         # notice.
                         time.sleep(ANTI_BUZZ_DELAY)
+                elif self.status == "disconnected" \
+                         and time.time() > self.last_xmit + DISCONNECT_TTL:
+                    # Queue is nonempty, but the IRC server might be
+                    # down. Letting failed connections retain queue
+                    # space forever would be a memory leak.
+                    self.status = "expired"
+                    break
                 elif not self.connection:
                     # Queue is nonempty but server isn't connected.
                     with self.irker.library_lock:
@@ -230,13 +241,6 @@ class Connection:
                         # Don't buzz on the empty-queue test while we're
                         # handshaking
                         time.sleep(ANTI_BUZZ_DELAY)
-                elif self.status == "disconnected" \
-                         and time.time() > self.last_xmit + DISCONNECT_TTL:
-                    # Queue is nonempty, but the IRC server might be
-                    # down. Letting failed connections retain queue
-                    # space forever would be a memory leak.
-                    self.status = "expired"
-                    break
                 elif self.status == "unseen" \
                          and time.time() > self.last_xmit + UNSEEN_TTL:
                     # Nasty people could attempt a denial-of-service