Skip to content

Commit dc6fc9c

Browse files
author
Ruben Bridgewater
committed
Use connect_timeout also as the socket_timeout if explicitly provided
Fixes #587 Fixes #393 Closes #652 Closes #394
1 parent afc4989 commit dc6fc9c

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ once the connection has been established. Setting `enable_offline_queue` to
200200
with an error, or an error will be emitted if no callback is specified.
201201
* `retry_max_delay`: *null*; By default every time the client tries to connect and fails the reconnection delay almost doubles.
202202
This delay normally grows infinitely, but setting `retry_max_delay` limits it to the maximum value, provided in milliseconds.
203-
* `connect_timeout`: *86400000*; Setting `connect_timeout` limits total time for client to reconnect.
204-
The value is provided in milliseconds and is counted once the disconnect occurred. The last retry is going to happen exactly at the timeout time.
205-
That way the default is to try reconnecting until 24h passed.
203+
* `connect_timeout`: *3600000*; Setting `connect_timeout` limits total time for client to connect and reconnect.
204+
The value is provided in milliseconds and is counted from the moment on a new client is created / a connection is lost. The last retry is going to happen exactly at the timeout time.
205+
Default is to try connecting until the default system socket timeout has been exceeded and to try reconnecting until 1h passed.
206206
* `max_attempts`: *0*; By default client will try reconnecting until connected. Setting `max_attempts`
207207
limits total amount of connection tries. Setting this to 1 will prevent any reconnect tries.
208208
* `auth_pass`: *null*; If set, client will run redis auth command on connect.

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ Features
1313
- Refactored manual backpressure control ([@BridgeAR](https://github.com/BridgeAR))
1414
- Removed the high water mark and low water mark. Such a mechanism should be implemented by a user instead
1515
- The `drain` event is from now on only emitted if the stream really had to buffer
16+
- Reduced the default connect_timeout to be one hour instead of 24h ([@BridgeAR](https://github.com/BridgeAR))
1617

1718
Bugfixes
1819

1920
- Fixed a js parser error that could result in a timeout ([@BridgeAR](https://github.com/BridgeAR))
2021
- Fixed .multi / .batch used with Node.js 0.10.x not working properly after a reconnect ([@BridgeAR](https://github.com/BridgeAR))
2122
- Fixed fired but not yet returned commands not being rejected after a connection loss ([@BridgeAR](https://github.com/BridgeAR))
23+
- Fixed connect_timeout not respected if no connection has ever been established ([@gagle](https://github.com/gagle) & [@benjie](https://github.com/benjie))
2224

2325
## v.2.2.5 - 18 Oct, 2015
2426

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function RedisClient(stream, options) {
8585
this.max_attempts = options.max_attempts | 0;
8686
this.command_queue = new Queue(); // Holds sent commands to de-pipeline them
8787
this.offline_queue = new Queue(); // Holds commands issued but not able to be sent
88-
this.connect_timeout = +options.connect_timeout || 86400000; // 24 * 60 * 60 * 1000 ms
88+
this.connect_timeout = +options.connect_timeout || 3600000; // 60 * 60 * 1000 ms
8989
this.enable_offline_queue = options.enable_offline_queue === false ? false : true;
9090
this.retry_max_delay = +options.retry_max_delay || null;
9191
this.initialize_retry_vars();
@@ -108,6 +108,13 @@ util.inherits(RedisClient, events.EventEmitter);
108108
RedisClient.prototype.install_stream_listeners = function() {
109109
var self = this;
110110

111+
if (this.options.connect_timeout) {
112+
this.stream.setTimeout(this.connect_timeout, function () {
113+
self.retry_totaltime = self.connect_timeout;
114+
self.connection_gone('timeout');
115+
});
116+
}
117+
111118
this.stream.on('connect', function () {
112119
self.on_connect();
113120
});

test/connection.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,48 @@ describe("connection tests", function () {
125125

126126
describe("when not connected", function () {
127127

128+
it("emit an error after the socket timeout exceeded the connect_timeout time", function (done) {
129+
var connect_timeout = 1000; // in ms
130+
var client = redis.createClient({
131+
parser: parser,
132+
host: '1.2.3.4',
133+
connect_timeout: connect_timeout
134+
});
135+
assert(client.stream._events.timeout);
136+
assert.strictEqual(client.address, '1.2.3.4:6379');
137+
var time = Date.now();
138+
139+
client.on("reconnecting", function (params) {
140+
throw new Error('No reconnect, since no connection was ever established');
141+
});
142+
143+
client.on('error', function(err) {
144+
assert(/Redis connection in broken state: connection timeout.*?exceeded./.test(err.message));
145+
assert(Date.now() - time < connect_timeout + 50);
146+
done();
147+
});
148+
});
149+
150+
it("use the system socket timeout if the connect_timeout has not been provided", function () {
151+
var client = redis.createClient({
152+
parser: parser,
153+
host: '1.2.3.4'
154+
});
155+
assert(client.stream._events.timeout === undefined);
156+
});
157+
158+
it("clears the socket timeout after a connection has been established", function (done) {
159+
var client = redis.createClient({
160+
parser: parser,
161+
connect_timeout: 1000
162+
});
163+
assert.strictEqual(client.stream._idleTimeout, 1000);
164+
client.on('connect', function () {
165+
assert.strictEqual(client.stream._idleTimeout, -1);
166+
done();
167+
});
168+
});
169+
128170
it("connect with host and port provided in the options object", function (done) {
129171
client = redis.createClient({
130172
host: 'localhost',

0 commit comments

Comments
 (0)