1
0
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:
Ruben Bridgewater
2015-09-10 18:00:57 +02:00
parent f2ee8dbc9e
commit 1e0421ac3b
3 changed files with 23 additions and 22 deletions

View File

@@ -198,7 +198,7 @@ to maximum value, provided in milliseconds.
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.
That way the default is to try reconnecting until at least 24h passed.
* `max_attempts` defaults to `null`. By default client will try reconnecting until connected. Setting `max_attempts`
limits total amount of reconnects.
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.
* `family` defaults to `IPv4`. The client connects in IPv4 if not specified or if the DNS resolution returns an IPv4 address.
You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns modules how to use the family type.

View File

@@ -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);
};

View File

@@ -687,7 +687,7 @@ describe("The node_redis client", function () {
describe('true', function () {
it("does not return an error and enqueues operation", function (done) {
var client = redis.createClient(9999, null, {
max_attempts: 1,
max_attempts: 0,
parser: parser
});
@@ -715,7 +715,7 @@ describe("The node_redis client", function () {
it("does not emit an error and enqueues operation", function (done) {
var client = redis.createClient(9999, null, {
parser: parser,
max_attempts: 1,
max_attempts: 0,
enable_offline_queue: false
});