You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Support new option 'max_attempts' to specify a total number of connection retries
This commit is contained in:
22
index.js
22
index.js
@@ -35,6 +35,10 @@ function RedisClient(stream, options) {
|
|||||||
this.should_buffer = false;
|
this.should_buffer = false;
|
||||||
this.command_queue_high_water = this.options.command_queue_high_water || 1000;
|
this.command_queue_high_water = this.options.command_queue_high_water || 1000;
|
||||||
this.command_queue_low_water = this.options.command_queue_low_water || 0;
|
this.command_queue_low_water = this.options.command_queue_low_water || 0;
|
||||||
|
this.max_attempts = null;
|
||||||
|
if (options.max_attempts && !isNaN(options.max_attempts) && options.max_attempts > 0) {
|
||||||
|
this.max_attempts = +options.max_attempts;
|
||||||
|
}
|
||||||
this.command_queue = new Queue(); // holds sent commands to de-pipeline them
|
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.offline_queue = new Queue(); // holds commands issued but not able to be sent
|
||||||
this.commands_sent = 0;
|
this.commands_sent = 0;
|
||||||
@@ -168,6 +172,7 @@ RedisClient.prototype.on_connect = function () {
|
|||||||
this.connections += 1;
|
this.connections += 1;
|
||||||
this.command_queue = new Queue();
|
this.command_queue = new Queue();
|
||||||
this.emitted_end = false;
|
this.emitted_end = false;
|
||||||
|
this.max_attempts = 0;
|
||||||
this.retry_totaltime = 0;
|
this.retry_totaltime = 0;
|
||||||
this.retry_timer = null;
|
this.retry_timer = null;
|
||||||
this.current_retry_delay = this.retry_time;
|
this.current_retry_delay = this.retry_time;
|
||||||
@@ -347,10 +352,19 @@ RedisClient.prototype.connection_gone = function (why) {
|
|||||||
if (exports.debug_mode) {
|
if (exports.debug_mode) {
|
||||||
console.log("Retry connection in " + this.current_retry_delay + " ms");
|
console.log("Retry connection in " + this.current_retry_delay + " ms");
|
||||||
}
|
}
|
||||||
this.attempts += 1;
|
|
||||||
this.emit("reconnecting", {
|
if (self.max_attempts && self.attempts >= self.max_attempts) {
|
||||||
delay: this.current_retry_delay,
|
self.retry_timer = null;
|
||||||
attempt: this.attempts
|
if (exports.debug_mode) {
|
||||||
|
console.log("Aborting connection attempt: Max attempts " + self.max_attempts + " failed.");
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.attempts += 1;
|
||||||
|
self.emit("reconnecting", {
|
||||||
|
delay: self.retry_delay,
|
||||||
|
attempt: self.attempts
|
||||||
});
|
});
|
||||||
this.retry_timer = setTimeout(function () {
|
this.retry_timer = setTimeout(function () {
|
||||||
if (exports.debug_mode) {
|
if (exports.debug_mode) {
|
||||||
|
Reference in New Issue
Block a user