diff --git a/changelog.md b/changelog.md index c530174e23..63a29bc4b7 100644 --- a/changelog.md +++ b/changelog.md @@ -1,6 +1,12 @@ Changelog ========= +## v0.7.1 - November 15, 2011 + +Fix regression in reconnect logic. + +Very much need automated tests for reconnection and queue logic. + ## v0.7.0 - November 14, 2011 Many contributed fixes. Thanks everybody. diff --git a/index.js b/index.js index fcee3308f2..5edd3bffb6 100644 --- a/index.js +++ b/index.js @@ -33,7 +33,6 @@ function RedisClient(stream, options) { this.connected = false; this.ready = false; this.connections = 0; - this.attempts = 1; this.should_buffer = false; this.command_queue_high_water = this.options.command_queue_high_water || 1000; this.command_queue_low_water = this.options.command_queue_low_water || 0; @@ -48,9 +47,7 @@ function RedisClient(stream, options) { if (options.connect_timeout && !isNaN(options.connect_timeout) && options.connect_timeout > 0) { this.connect_timeout = +options.connect_timeout; } - this.retry_totaltime = 0; - this.retry_delay = 250; - this.retry_backoff = 1.7; + this.initialize_retry_vars(); this.subscriptions = false; this.monitoring = false; this.closing = false; @@ -91,6 +88,14 @@ function RedisClient(stream, options) { util.inherits(RedisClient, events.EventEmitter); exports.RedisClient = RedisClient; +RedisClient.prototype.initialize_retry_vars = function () { + this.retry_timer = null; + this.retry_totaltime = 0; + this.retry_delay = 250; + this.retry_backoff = 1.7; + this.attempts = 1; +}; + // flush offline_queue and command_queue, erroring any items with a callback first RedisClient.prototype.flush_and_error = function (message) { var command_obj; @@ -194,10 +199,7 @@ RedisClient.prototype.on_connect = function () { this.connections += 1; this.command_queue = new Queue(); this.emitted_end = false; - this.max_attempts = 0; - this.retry_totaltime = 0; - this.retry_timer = null; - this.current_retry_delay = this.retry_delay; + this.initialize_retry_vars(); this.stream.setNoDelay(); this.stream.setTimeout(0); @@ -373,7 +375,7 @@ RedisClient.prototype.connection_gone = function (why) { return; } - this.current_retry_delay = this.current_retry_delay * this.retry_backoff; + this.retry_delay = Math.floor(this.retry_delay * this.retry_backoff); if (exports.debug_mode) { console.log("Retry connection in " + this.current_retry_delay + " ms"); @@ -387,8 +389,8 @@ RedisClient.prototype.connection_gone = function (why) { return; } - self.attempts += 1; - self.emit("reconnecting", { + this.attempts += 1; + this.emit("reconnecting", { delay: self.retry_delay, attempt: self.attempts }); @@ -408,7 +410,7 @@ RedisClient.prototype.connection_gone = function (why) { self.stream.connect(self.port, self.host); self.retry_timer = null; - }, this.current_retry_delay); + }, this.retry_delay); }; RedisClient.prototype.on_data = function (data) { diff --git a/package.json b/package.json index 75f561c917..cf1248cd18 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name" : "redis", - "version" : "0.7.0", + "version" : "0.7.1", "description" : "Redis client library", "author": "Matt Ranney ", "contributors": [ diff --git a/tests/reconnect_test.js b/tests/reconnect_test.js index df5b4b256d..7abdd51665 100644 --- a/tests/reconnect_test.js +++ b/tests/reconnect_test.js @@ -1,5 +1,5 @@ var redis = require("../index").createClient(null, null, { - max_attempts: 2 +// max_attempts: 4 }); redis.on("error", function (err) { @@ -13,9 +13,6 @@ redis.on("ready", function () { redis.on("reconnecting", function (arg) { console.log("Redis reconnecting: " + JSON.stringify(arg)); }); -redis.on("not_reconnecting", function (arg) { - console.log("Redis NOT reconnecting: " + arg); -}); redis.on("connect", function () { console.log("Redis connected."); });