diff --git a/README.md b/README.md index 7e75d5e630..528a0b894c 100644 --- a/README.md +++ b/README.md @@ -195,8 +195,8 @@ with an error, or an error will be thrown if no callback is specified. reconnection (delay) almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits delay to maximum value, provided in milliseconds. * `connect_timeout` defaults to `86400000`. Setting `connect_timeout` limits total time for client to reconnect. -Value is provided in milliseconds and is counted once the disconnect occured. The last retry is going to happen once after the connect_timeout value is exceeded. -That way the default is to try reconnecting until at least 24h passed. +Value is provided in milliseconds and is counted once the disconnect occured. The last retry is going to happen exactly at the timeout time. +That way the default is to try reconnecting until 24h passed. * `max_attempts` defaults to `null`. By default client will try reconnecting until connected. Setting `max_attempts` limits total amount of reconnects. Setting this to 0 will prevent any reconnect tries. * `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect. diff --git a/index.js b/index.js index db6ca5154a..e20577a684 100644 --- a/index.js +++ b/index.js @@ -423,7 +423,7 @@ RedisClient.prototype.connection_gone = function (why) { return; } - if (this.retry_totaltime > this.connect_timeout) { + if (this.retry_totaltime >= this.connect_timeout) { this.emit('error', new Error("Redis connection in broken state: connection timeout exceeded.")); return; } @@ -459,11 +459,12 @@ RedisClient.prototype.connection_gone = function (why) { return; } - var nextDelay = Math.floor(this.retry_delay * this.retry_backoff); - if (this.retry_max_delay !== null && nextDelay > this.retry_max_delay) { + this.retry_delay = Math.floor(this.retry_delay * this.retry_backoff); + if (this.retry_max_delay !== null && this.retry_delay > this.retry_max_delay) { this.retry_delay = this.retry_max_delay; - } else { - this.retry_delay = nextDelay; + } else if (this.retry_totaltime + this.retry_delay > this.connect_timeout) { + // Do not exceed the maximum + this.retry_delay = this.connect_timeout - this.retry_totaltime; } debug("Retry connection in " + this.retry_delay + " ms"); diff --git a/test/connection.spec.js b/test/connection.spec.js index af48d2f9d6..d76a059941 100644 --- a/test/connection.spec.js +++ b/test/connection.spec.js @@ -45,7 +45,6 @@ describe("on lost connection", function () { connect_timeout: connect_timeout }); var time = 0; - var multiplier = 0; client.once('ready', function() { // Pretend that redis can't reconnect @@ -54,17 +53,13 @@ describe("on lost connection", function () { }); client.on("reconnecting", function (params) { - if (time > 0 && multiplier === 0) { - multiplier = params.delay / time; - } time += params.delay; }); client.on('error', function(err) { if (/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message)) { setTimeout(function () { - assert(time > connect_timeout); - assert(time / multiplier < connect_timeout); + assert(time === connect_timeout); done(); }, 1500); }