Skip to content

Commit

Permalink
Fix regression in reconnect logic.
Browse files Browse the repository at this point in the history
Very much need automated tests for reconnection and queue logic.
  • Loading branch information
mranney committed Nov 15, 2011
1 parent 5834f63 commit e39e842
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
26 changes: 14 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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");
Expand All @@ -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
});
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{ "name" : "redis",
"version" : "0.7.0",
"version" : "0.7.1",
"description" : "Redis client library",
"author": "Matt Ranney <[email protected]>",
"contributors": [
Expand Down
5 changes: 1 addition & 4 deletions tests/reconnect_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var redis = require("../index").createClient(null, null, {
max_attempts: 2
// max_attempts: 4
});

redis.on("error", function (err) {
Expand All @@ -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.");
});
Expand Down

0 comments on commit e39e842

Please sign in to comment.