Skip to content

Commit 9584ce6

Browse files
committed
Add socket_connect_timeout option.
1 parent de66927 commit 9584ce6

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ every command on a client.
198198
Nagle algorithm on the underlying socket. Setting this option to `false` can result in additional throughput at the
199199
cost of more latency. Most applications will want this set to `true`.
200200
* `socket_keepalive` defaults to `true`. Whether the keep-alive functionality is enabled on the underlying socket.
201+
* `socket_connect_timeout` defaults to `null`. By default a connection attempts use the operating system's timeout, but
202+
setting `socket_connect_timeout` will time-out the connection attempt after this many milliseconds instead.
201203
* `no_ready_check`: defaults to `false`. When a connection is established to the Redis server, the server might still
202204
be loading the database from disk. While loading, the server not respond to any commands. To work around this,
203205
`node_redis` has a "ready check" which sends the `INFO` command to the server. The response from the `INFO` command

index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ exports.RedisClient = RedisClient;
9494
RedisClient.prototype.install_stream_listeners = function() {
9595
var self = this;
9696

97+
if (this.options.socket_connect_timeout > 0) {
98+
this.stream.setTimeout(this.options.socket_connect_timeout, function () {
99+
self.on_timeout();
100+
});
101+
}
102+
97103
this.stream.on("connect", function () {
98104
self.on_connect();
99105
});
@@ -247,6 +253,20 @@ RedisClient.prototype.do_auth = function () {
247253
self.send_anyway = false;
248254
};
249255

256+
RedisClient.prototype.on_timeout = function () {
257+
if (this.connected || this.closing) {
258+
// Only handle connection timeouts.
259+
return;
260+
}
261+
262+
if (exports.debug_mode) {
263+
console.log("Stream timeout " + this.address + " id " + this.connection_id);
264+
}
265+
266+
this.stream.end();
267+
this.connection_gone("timeout");
268+
};
269+
250270
RedisClient.prototype.on_connect = function () {
251271
if (exports.debug_mode) {
252272
console.log("Stream connected " + this.address + " id " + this.connection_id);

0 commit comments

Comments
 (0)