Skip to content

Commit 2bc1dbe

Browse files
committed
Add a way for configurable socket timeouts
1 parent 4f85030 commit 2bc1dbe

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ using unix sockets if possible to increase throughput.
178178
| 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. |
179179
| socket_keepalive | true | If set to `true`, the keep-alive functionality is enabled on the underlying socket. |
180180
| socket_initial_delay | 0 | Initial Delay in milliseconds, and this will also behave the interval keep alive message sending to Redis. |
181+
| 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. |
181182
| 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. |
182183
| 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. |
183184
| retry_unfulfilled_commands | false | If set to `true`, all commands that were unfulfilled while the connection is lost will be retried after the connection has been reestablished. Use this with caution if you use state altering commands (e.g. `incr`). This is especially useful if you use blocking commands. |

index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ function RedisClient (options, stream) {
7979
options.socket_initial_delay = 0;
8080
// set default to 0, which is aligned to https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay
8181
}
82+
if (options.socket_timeout === undefined) {
83+
options.socket_timeout = 0;
84+
}
8285
for (var command in options.rename_commands) {
8386
options.rename_commands[command.toLowerCase()] = options.rename_commands[command];
8487
}
@@ -353,7 +356,14 @@ RedisClient.prototype.on_connect = function () {
353356
this.ready = false;
354357
this.emitted_end = false;
355358
this.stream.setKeepAlive(this.options.socket_keepalive, this.options.socket_initial_delay);
356-
this.stream.setTimeout(0);
359+
this.stream.setKeepAlive(this.options.socket_keepalive, this.options.socket_initialdelay);
360+
if (this.options.socket_timeout === 0) {
361+
this.stream.setTimeout(0);
362+
} else {
363+
this.stream.setTimeout(this.options.socket_timeout, function() {
364+
this.connection_gone('timeout');
365+
});
366+
}
357367

358368
this.emit('connect');
359369
this.initialize_retry_vars();

0 commit comments

Comments
 (0)