Skip to content

Commit 3eea37d

Browse files
committed
Add a way for configurable socket timeouts
1 parent 12265a5 commit 3eea37d

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ __Note:__ Using `'rediss://...` for the protocol in a `redis_url` will enable a
247247
| return_buffers | false | If set to `true`, then all replies will be sent to callbacks as Buffers instead of Strings. |
248248
| detect_buffers | false | If set to `true`, then replies will be sent to callbacks as Buffers. This option lets you switch between Buffers and Strings on a per-command basis, whereas `return_buffers` applies to every command on a client. __Note__: This doesn't work properly with the pubsub mode. A subscriber has to either always return Strings or Buffers. |
249249
| socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. |
250+
| socket_timeout | 0 | If set to `0` never timeout the underlying socket connection once connected. Otherwise a time in ms to timeout if the underlying socket is idle. |
250251
| socket_initialdelay | 0 | Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis. |
251252
| no_ready_check | false | When a connection is established to the Redis server, the server might still be loading the database from disk. While loading, the server will not respond to any commands. To work around this, `node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command indicates whether the server is ready for more commands. When ready, `node_redis` emits a `ready` event. Setting `no_ready_check` to `true` will inhibit this check. |
252253
| enable_offline_queue | true | By default, if there is no active 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 executed immediately with an error, or an error will be emitted if no callback is specified. |

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ function RedisClient (options, stream) {
101101
if (options.socket_keepalive === undefined) {
102102
options.socket_keepalive = true;
103103
}
104+
if (options.socket_timeout === undefined) {
105+
options.socket_timeout = 0;
106+
}
104107
if (options.socket_initialdelay === undefined) {
105108
options.socket_initialdelay = 0;
106109
// set default to 0, which is aligned to https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay
@@ -415,7 +418,13 @@ RedisClient.prototype.on_connect = function () {
415418
this.ready = false;
416419
this.emitted_end = false;
417420
this.stream.setKeepAlive(this.options.socket_keepalive, this.options.socket_initialdelay);
418-
this.stream.setTimeout(0);
421+
if (this.options.socket_timeout === 0) {
422+
this.stream.setTimeout(0);
423+
} else {
424+
this.stream.setTimeout(this.options.socket_timeout, function() {
425+
this.connection_gone('timeout');
426+
});
427+
}
419428

420429
this.emit('connect');
421430
this.initialize_retry_vars();

0 commit comments

Comments
 (0)