From 389d592129ece20cbf1c5960bb5cfa00c5115853 Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Wed, 11 Jul 2012 12:35:24 -0700 Subject: [PATCH] Set node status to "reconnecting" until at least one failed reconnect --- index.js | 12 ++++++++++++ lib/node.js | 12 ++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 69e4541..b5b6472 100644 --- a/index.js +++ b/index.js @@ -434,6 +434,18 @@ RedisHAClient.prototype.parseNodeList = function(nodeList, options) { } } }); + node.on('reconnecting', function() { + if (self.isMaster(this)) { + self.warn('MASTER connection dropped, reconnecting...'); + } + else { + self.warn(this + ' connection dropped, reconnecting...'); + } + self.connected = false; + self.ready = false; + // @todo: pass some real attempts/timers here + self.emit('reconnecting', {}); + }); node.on('down', function() { if (self.isMaster(this)) { self.error('MASTER is down! (' + this + ')'); diff --git a/lib/node.js b/lib/node.js index b93a440..16384e6 100644 --- a/lib/node.js +++ b/lib/node.js @@ -66,7 +66,15 @@ Node.prototype.connect = function() { }); this.client.on('end', function() { if (!this.closing) { - self.setStatus('down'); + self.setStatus('reconnecting'); + // Client connection closed without quit(). If reconnection fails, the + // node is considered down. + setTimeout(function waitForReconnect() { + if (!self.client.connected) { + self.emit('error', new Error(self + ' connection dropped and reconnection failed!')); + self.setStatus('down'); + } + }, Math.floor(this.retry_delay * this.retry_backoff * 2)); } }); this.client.on('connect', function() { @@ -83,7 +91,7 @@ Node.prototype.connect = function() { self.setStatus('up'); } else { - console.warn('WARNING! ' + self.toString() + ' has slave-read-only mode ON, which breaks haredis failover! slave-read-only automatically turned OFF.'); + console.warn('WARNING! ' + self + ' has slave-read-only mode ON, which breaks haredis failover! slave-read-only automatically turned OFF.'); self.client.CONFIG('SET', 'slave-read-only', 'no', function(err, reply) { if (err) { return self.emit('error', err);