1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Try exactly until the connection timeout has been reached

Fixes #587
This commit is contained in:
Ruben Bridgewater
2015-09-10 18:40:43 +02:00
parent a9e7663aff
commit 3c2ba8c373
3 changed files with 9 additions and 13 deletions

View File

@@ -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 reconnection (delay) almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits delay
to maximum value, provided in milliseconds. to maximum value, provided in milliseconds.
* `connect_timeout` defaults to `86400000`. Setting `connect_timeout` limits total time for client to reconnect. * `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. 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 at least 24h passed. 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` * `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. 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. * `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect.

View File

@@ -423,7 +423,7 @@ RedisClient.prototype.connection_gone = function (why) {
return; 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.")); this.emit('error', new Error("Redis connection in broken state: connection timeout exceeded."));
return; return;
} }
@@ -459,11 +459,12 @@ RedisClient.prototype.connection_gone = function (why) {
return; return;
} }
var nextDelay = Math.floor(this.retry_delay * this.retry_backoff); this.retry_delay = Math.floor(this.retry_delay * this.retry_backoff);
if (this.retry_max_delay !== null && nextDelay > this.retry_max_delay) { if (this.retry_max_delay !== null && this.retry_delay > this.retry_max_delay) {
this.retry_delay = this.retry_max_delay; this.retry_delay = this.retry_max_delay;
} else { } else if (this.retry_totaltime + this.retry_delay > this.connect_timeout) {
this.retry_delay = nextDelay; // Do not exceed the maximum
this.retry_delay = this.connect_timeout - this.retry_totaltime;
} }
debug("Retry connection in " + this.retry_delay + " ms"); debug("Retry connection in " + this.retry_delay + " ms");

View File

@@ -45,7 +45,6 @@ describe("on lost connection", function () {
connect_timeout: connect_timeout connect_timeout: connect_timeout
}); });
var time = 0; var time = 0;
var multiplier = 0;
client.once('ready', function() { client.once('ready', function() {
// Pretend that redis can't reconnect // Pretend that redis can't reconnect
@@ -54,17 +53,13 @@ describe("on lost connection", function () {
}); });
client.on("reconnecting", function (params) { client.on("reconnecting", function (params) {
if (time > 0 && multiplier === 0) {
multiplier = params.delay / time;
}
time += params.delay; time += params.delay;
}); });
client.on('error', function(err) { client.on('error', function(err) {
if (/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message)) { if (/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message)) {
setTimeout(function () { setTimeout(function () {
assert(time > connect_timeout); assert(time === connect_timeout);
assert(time / multiplier < connect_timeout);
done(); done();
}, 1500); }, 1500);
} }