You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Merge branch 'add-retry-max' of https://github.com/tomaszdurka/node_redis into tomaszdurka-add-retry-max
This commit is contained in:
@@ -202,6 +202,9 @@ connection to the redis server, commands are added to a queue and are executed
|
|||||||
once the connection has been established. Setting `enable_offline_queue` to
|
once the connection has been established. Setting `enable_offline_queue` to
|
||||||
`false` will disable this feature and the callback will be execute immediately
|
`false` will disable this feature and the callback will be execute immediately
|
||||||
with an error, or an error will be thrown if no callback is specified.
|
with an error, or an error will be thrown if no callback is specified.
|
||||||
|
* `retry_max_delay`: defaults to `null`. By default every time the client tries to connect and fails time before
|
||||||
|
reconnection (delay) almost doubles. This delay normally grows infinitely, but setting `retry_max_delay` limits delay
|
||||||
|
to maximum value, provided in miliseconds.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var redis = require("redis"),
|
var redis = require("redis"),
|
||||||
|
11
index.js
11
index.js
@@ -51,11 +51,14 @@ function RedisClient(stream, options) {
|
|||||||
if (options.connect_timeout && !isNaN(options.connect_timeout) && options.connect_timeout > 0) {
|
if (options.connect_timeout && !isNaN(options.connect_timeout) && options.connect_timeout > 0) {
|
||||||
this.connect_timeout = +options.connect_timeout;
|
this.connect_timeout = +options.connect_timeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.enable_offline_queue = true;
|
this.enable_offline_queue = true;
|
||||||
if (typeof this.options.enable_offline_queue === "boolean") {
|
if (typeof this.options.enable_offline_queue === "boolean") {
|
||||||
this.enable_offline_queue = this.options.enable_offline_queue;
|
this.enable_offline_queue = this.options.enable_offline_queue;
|
||||||
}
|
}
|
||||||
|
this.retry_max_delay = null;
|
||||||
|
if (options.retry_max_delay !== undefined && !isNaN(options.retry_max_delay) && options.retry_max_delay > 0) {
|
||||||
|
this.retry_max_delay = options.retry_max_delay;
|
||||||
|
}
|
||||||
|
|
||||||
this.initialize_retry_vars();
|
this.initialize_retry_vars();
|
||||||
this.pub_sub_mode = false;
|
this.pub_sub_mode = false;
|
||||||
@@ -429,7 +432,11 @@ RedisClient.prototype.connection_gone = function (why) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 = Math.floor(this.retry_delay * this.retry_backoff);
|
||||||
|
}
|
||||||
|
|
||||||
if (exports.debug_mode) {
|
if (exports.debug_mode) {
|
||||||
console.log("Retry connection in " + this.retry_delay + " ms");
|
console.log("Retry connection in " + this.retry_delay + " ms");
|
||||||
|
22
test.js
22
test.js
@@ -1846,6 +1846,28 @@ tests.auth = function () {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
tests.reconnectRetryMaxDelay = function() {
|
||||||
|
var time = new Date().getTime(),
|
||||||
|
name = 'reconnectRetryMaxDelay',
|
||||||
|
reconnecting = false;
|
||||||
|
var client = redis.createClient(PORT, HOST, {
|
||||||
|
retry_max_delay: 1
|
||||||
|
});
|
||||||
|
client.on('ready', function() {
|
||||||
|
if (!reconnecting) {
|
||||||
|
reconnecting = true;
|
||||||
|
client.retry_delay = 1000;
|
||||||
|
client.retry_backoff = 1;
|
||||||
|
client.stream.end();
|
||||||
|
} else {
|
||||||
|
client.end();
|
||||||
|
var lasted = new Date().getTime() - time;
|
||||||
|
assert.ok(lasted < 1000);
|
||||||
|
next(name);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
all_tests = Object.keys(tests);
|
all_tests = Object.keys(tests);
|
||||||
all_start = new Date();
|
all_start = new Date();
|
||||||
test_count = 0;
|
test_count = 0;
|
||||||
|
Reference in New Issue
Block a user