From 38dbacac9f0604d8754e3e98f2c452e3c1494252 Mon Sep 17 00:00:00 2001 From: Tomasz Durka Date: Tue, 12 Mar 2013 12:31:19 +0100 Subject: [PATCH 1/3] Add retry_max_delay option - add option - add test --- index.js | 11 +++++++++-- test.js | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4135dc1ec9..3b5ee8f042 100644 --- a/index.js +++ b/index.js @@ -51,11 +51,14 @@ function RedisClient(stream, options) { if (options.connect_timeout && !isNaN(options.connect_timeout) && options.connect_timeout > 0) { this.connect_timeout = +options.connect_timeout; } - this.enable_offline_queue = true; if (typeof this.options.enable_offline_queue === "boolean") { 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.pub_sub_mode = false; @@ -429,7 +432,11 @@ RedisClient.prototype.connection_gone = function (why) { 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) { console.log("Retry connection in " + this.retry_delay + " ms"); diff --git a/test.js b/test.js index b790f5f64d..12947e25cc 100644 --- a/test.js +++ b/test.js @@ -1798,6 +1798,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_start = new Date(); test_count = 0; From 0698a5e627a2396ba768650665e115443e46f921 Mon Sep 17 00:00:00 2001 From: Tomasz Durka Date: Sun, 17 Mar 2013 23:45:51 +0100 Subject: [PATCH 2/3] Formatting --- index.js | 18 +++++++++--------- test.js | 38 +++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 3b5ee8f042..2ef865987e 100644 --- a/index.js +++ b/index.js @@ -55,10 +55,10 @@ function RedisClient(stream, options) { if (typeof this.options.enable_offline_queue === "boolean") { 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.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.pub_sub_mode = false; @@ -432,11 +432,11 @@ RedisClient.prototype.connection_gone = function (why) { return; } - 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 (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) { console.log("Retry connection in " + this.retry_delay + " ms"); diff --git a/test.js b/test.js index 12947e25cc..48d352e7b0 100644 --- a/test.js +++ b/test.js @@ -1799,25 +1799,25 @@ 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); - } - }); + 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); From 375929103431a053f6d3ac2abf13ddaec3b57ef6 Mon Sep 17 00:00:00 2001 From: Tomasz Durka Date: Sun, 17 Mar 2013 23:52:17 +0100 Subject: [PATCH 3/3] Docu --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 82fed0b080..bd1cc3b503 100644 --- a/README.md +++ b/README.md @@ -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 `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. +* `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 var redis = require("redis"),