Skip to content

Commit

Permalink
Support new option 'connect_timeout' to stop connection retries after…
Browse files Browse the repository at this point in the history
… the number of ms specified
  • Loading branch information
orls authored and mranney committed Nov 10, 2011
1 parent 8a2c1ad commit 025c2e9
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ parsers.push(require("./lib/parser/javascript"));

function RedisClient(stream, options) {
this.stream = stream;
this.options = options || {};
this.options = options = options || {};

this.connected = false;
this.ready = false;
Expand All @@ -38,9 +38,13 @@ function RedisClient(stream, options) {
this.command_queue = new Queue(); // holds sent commands to de-pipeline them
this.offline_queue = new Queue(); // holds commands issued but not able to be sent
this.commands_sent = 0;
this.retry_delay = 250; // inital reconnection delay
this.current_retry_delay = this.retry_delay;
this.retry_backoff = 1.7; // each retry waits current delay * retry_backoff
this.connect_timeout = false;
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.subscriptions = false;
this.monitoring = false;
this.closing = false;
Expand Down Expand Up @@ -164,6 +168,7 @@ RedisClient.prototype.on_connect = function () {
this.connections += 1;
this.command_queue = new Queue();
this.emitted_end = false;
this.retry_totaltime = 0;
this.retry_timer = null;
this.current_retry_delay = this.retry_time;
this.stream.setNoDelay();
Expand Down Expand Up @@ -351,6 +356,18 @@ RedisClient.prototype.connection_gone = function (why) {
if (exports.debug_mode) {
console.log("Retrying connection...");
}

self.retry_delay = self.retry_delay * self.retry_backoff;
self.retry_totaltime += self.retry_delay;

if (self.connect_timeout && self.retry_totaltime >= self.connect_timeout) {
self.retry_timer = null;
if (exports.debug_mode) {
console.log("Aborting connection attempt: Total timeout of " + self.connect_timeout + "ms exceeded.");
}
return;
}

self.stream.connect(self.port, self.host);
self.retry_timer = null;
}, this.current_retry_delay);
Expand Down

0 comments on commit 025c2e9

Please sign in to comment.