From 07279373521c9977145299ce2b124b230440d32f Mon Sep 17 00:00:00 2001 From: Toby Fox Date: Thu, 26 Jun 2014 19:30:47 -0400 Subject: [PATCH] Emitting error event after hitting reconnect max_attempts or connect_timeout Also documenting the "reconnecting" event --- README.md | 5 +++++ index.js | 5 ++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d1ae32e26bf..ea29b96a8bd 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,11 @@ resume sending when you get `drain`. `client` will emit `idle` when there are no outstanding commands that are awaiting a response. +### "reconnecting" + +`client` will emit `reconnecting` when trying to reconnect to the Redis server after losing the connection. Listeners +are passed an object containing `delay` (in ms) and `attempt` (the attempt #) attributes. + ## redis.createClient(port, host, options) Create a new client connection. `port` defaults to `6379` and `host` defaults diff --git a/index.js b/index.js index 9674e7a5282..9a2a7d58a5e 100644 --- a/index.js +++ b/index.js @@ -508,8 +508,10 @@ RedisClient.prototype.connection_gone = function (why) { if (this.max_attempts && this.attempts >= this.max_attempts) { this.retry_timer = null; // TODO - some people need a "Redis is Broken mode" for future commands that errors immediately, and others - // want the program to exit. Right now, we just log, which doesn't really help in either case. + // want the program to exit. Right now, we just log, which doesn't really help in either case, and emit + // an error event. console.error("node_redis: Couldn't get Redis connection after " + this.max_attempts + " attempts."); + this.emit('error', new Error("Redis connection in broken state")); return; } @@ -529,6 +531,7 @@ RedisClient.prototype.connection_gone = function (why) { self.retry_timer = null; // TODO - engage Redis is Broken mode for future commands, or whatever console.error("node_redis: Couldn't get Redis connection after " + self.retry_totaltime + "ms."); + this.emit('error', new Error("Redis connection in broken state")); return; }