You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Emit errors if the connection timeout / maximum retry attempts have been exceeded
Accept setting max_attempts to zero. The reconnection event is now emitted when trying to reconnect instead of earlier. The connection timeout is now going to trigger once after exceeding the maximum timeout instead of stopping earlier.
This commit is contained in:
39
index.js
39
index.js
@@ -52,7 +52,8 @@ function RedisClient(stream, options) {
|
||||
this.should_buffer = false;
|
||||
this.command_queue_high_water = this.options.command_queue_high_water || 1000;
|
||||
this.command_queue_low_water = this.options.command_queue_low_water || 0;
|
||||
if (options.max_attempts && options.max_attempts > 0) {
|
||||
this.max_attempts = null;
|
||||
if (options.max_attempts || options.max_attempts === 0 || options.max_attempts === '0') {
|
||||
this.max_attempts = +options.max_attempts;
|
||||
}
|
||||
this.command_queue = new Queue(); // holds sent commands to de-pipeline them
|
||||
@@ -419,6 +420,16 @@ RedisClient.prototype.connection_gone = function (why) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.max_attempts !== null && this.attempts > this.max_attempts) {
|
||||
this.emit('error', new Error("Redis connection in broken state: maximum connection attempts exceeded."));
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.retry_totaltime > this.connect_timeout) {
|
||||
this.emit('error', new Error("Redis connection in broken state: connection timeout exceeded."));
|
||||
return;
|
||||
}
|
||||
|
||||
debug("Redis connection is gone from " + why + " event.");
|
||||
this.connected = false;
|
||||
this.ready = false;
|
||||
@@ -436,7 +447,7 @@ RedisClient.prototype.connection_gone = function (why) {
|
||||
}
|
||||
|
||||
// since we are collapsing end and close, users don't expect to be called twice
|
||||
if (! this.emitted_end) {
|
||||
if (!this.emitted_end) {
|
||||
this.emit("end");
|
||||
this.emitted_end = true;
|
||||
}
|
||||
@@ -459,30 +470,20 @@ RedisClient.prototype.connection_gone = function (why) {
|
||||
|
||||
debug("Retry connection in " + this.retry_delay + " ms");
|
||||
|
||||
if (this.max_attempts && this.attempts >= this.max_attempts) {
|
||||
this.retry_timer = null;
|
||||
this.emit('error', new Error("Redis connection in broken state: maximum connection attempts exceeded."));
|
||||
return;
|
||||
}
|
||||
|
||||
this.attempts += 1;
|
||||
this.emit("reconnecting", {
|
||||
delay: self.retry_delay,
|
||||
attempt: self.attempts
|
||||
});
|
||||
this.retry_timer = setTimeout(function () {
|
||||
debug("Retrying connection...");
|
||||
|
||||
self.retry_totaltime += self.retry_delay;
|
||||
self.emit("reconnecting", {
|
||||
delay: self.retry_delay,
|
||||
attempt: self.attempts
|
||||
});
|
||||
|
||||
if (self.connect_timeout && self.retry_totaltime >= self.connect_timeout) {
|
||||
self.retry_timer = null;
|
||||
this.emit('error', new Error("Redis connection in broken state: connection timeout exceeded."));
|
||||
return;
|
||||
}
|
||||
self.retry_totaltime += self.retry_delay;
|
||||
self.attempts += 1;
|
||||
|
||||
self.stream = net.createConnection(self.connectionOption);
|
||||
self.install_stream_listeners();
|
||||
|
||||
self.retry_timer = null;
|
||||
}, this.retry_delay);
|
||||
};
|
||||
|
Reference in New Issue
Block a user