Better IRC server reconnection logic
authorDarren <darren@darrenwhitlen.com>
Sat, 26 Jul 2014 11:20:11 +0000 (12:20 +0100)
committerDarren <darren@darrenwhitlen.com>
Sat, 26 Jul 2014 11:20:11 +0000 (12:20 +0100)
server/irc/connection.js

index e19ef59fdbd6d72d17a4fea7de0e7a66b730fac3..97cf2728240f4946ae8a88671839cb28463b73d4 100644 (file)
@@ -40,6 +40,9 @@ var IrcConnection = function (hostname, port, ssl, nick, user, options, state, c
     // If the connection closes and this is false, we reconnect
     this.requested_disconnect = false;
 
+    // Number of times we have tried to reconnect
+    this.reconnect_attempts = 0;
+
     // IRCd write buffers (flood controll)
     this.write_buffer = [];
 
@@ -308,12 +311,24 @@ IrcConnection.prototype.connect = function () {
                 delete global.clients.port_pairs[that.identd_port_pair];
             }
 
-            should_reconnect = (!that.requested_disconnect && was_connected && had_registered);
+            // If trying to reconnect, continue with it
+            if (that.reconnect_attempts && that.reconnect_attempts < 3) {
+                should_reconnect = true;
+
+            // If this was an unplanned disconnect and we were originally connected OK, reconnect
+            } else if (!that.requested_disconnect  && was_connected && had_registered) {
+                should_reconnect = true;
+
+            } else {
+                should_reconnect = false;
+            }
 
             if (should_reconnect) {
+                that.reconnect_attempts++;
                 that.emit('reconnecting');
             } else {
                 that.emit('close', had_error);
+                that.reconnect_attempts = 0;
             }
 
             // Close the whole socket down
@@ -324,7 +339,7 @@ IrcConnection.prototype.connect = function () {
             if (should_reconnect) {
                 setTimeout(function() {
                     that.connect();
-                }, 3000);
+                }, 4000);
             }
         });
     });