Skip to content

Commit 9fe72a3

Browse files
committed
Add socket_connect_timeout option.
1 parent 1b8201e commit 9fe72a3

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
@@ -181,6 +181,8 @@ every command on a client.
181181
Nagle algorithm on the underlying socket. Setting this option to `false` can result in additional throughput at the
182182
cost of more latency. Most applications will want this set to `true`.
183183
* `socket_keepalive` defaults to `true`. Whether the keep-alive functionality is enabled on the underlying socket.
184+
* `socket_connect_timeout` defaults to `null`. By default a connection attempts use the operating system's timeout, but
185+
setting `socket_connect_timeout` will time-out the connection attempt after this many milliseconds instead.
184186
* `no_ready_check`: defaults to `false`. When a connection is established to the Redis server, the server might still
185187
be loading the database from disk. While loading, the server not respond to any commands. To work around this,
186188
`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
@@ -81,6 +81,12 @@ exports.RedisClient = RedisClient;
8181
RedisClient.prototype.install_stream_listeners = function() {
8282
var self = this;
8383

84+
if (this.options.socket_connect_timeout > 0) {
85+
this.stream.setTimeout(this.options.socket_connect_timeout, function () {
86+
self.on_timeout();
87+
});
88+
}
89+
8490
this.stream.on("connect", function () {
8591
self.on_connect();
8692
});
@@ -218,6 +224,20 @@ RedisClient.prototype.do_auth = function () {
218224
self.send_anyway = false;
219225
};
220226

227+
RedisClient.prototype.on_timeout = function () {
228+
if (this.connected || this.closing) {
229+
// Only handle connection timeouts.
230+
return;
231+
}
232+
233+
if (exports.debug_mode) {
234+
console.log("Stream timeout " + this.address + " id " + this.connection_id);
235+
}
236+
237+
this.stream.end();
238+
this.connection_gone("timeout");
239+
};
240+
221241
RedisClient.prototype.on_connect = function () {
222242
debug("Stream connected " + this.address + " id " + this.connection_id);
223243

0 commit comments

Comments
 (0)