Skip to content

Commit 1dc38bc

Browse files
committed
Merge pull request #726 from dougwilson/feature/connect-timeout
Add option for connect timeout
2 parents 5012a45 + f5c19be commit 1dc38bc

File tree

6 files changed

+70
-0
lines changed

6 files changed

+70
-0
lines changed

Changes.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ you spot any mistakes.
66

77
## HEAD
88

9+
* Add `connectTimeout` option to specify a timeout for establishing a connection #726
10+
911
## v2.0.1
1012

1113
* internal parser speed improvement #702

Readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ When establishing a connection, you can set the following options:
140140
* `database`: Name of the database to use for this connection (Optional).
141141
* `charset`: The charset for the connection. (Default: `'UTF8_GENERAL_CI'`. Value needs to be all in upper case letters!)
142142
* `timezone`: The timezone used to store local dates. (Default: `'local'`)
143+
* `connectTimeout`: The milliseconds before a timeout occurs during the initial connection
144+
to the MySQL server. (Default: no timeout)
143145
* `stringifyObjects`: Stringify objects instead of converting to values. See
144146
issue [#501](https://github.com/felixge/node-mysql/issues/501). (Default: `'false'`)
145147
* `insecureAuth`: Allow connecting to MySQL instances that ask for the old

lib/Connection.js

+23
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ Connection.prototype.connect = function(cb) {
7676
this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
7777
this._protocol.on('drain', this._handleProtocolDrain.bind(this));
7878
this._protocol.on('end', this._handleProtocolEnd.bind(this));
79+
80+
if (this.config.connectTimeout) {
81+
var handleConnectTimeout = this._handleConnectTimeout.bind(this);
82+
83+
this._socket.setTimeout(this.config.connectTimeout, handleConnectTimeout);
84+
this._socket.once('connect', function() {
85+
this.setTimeout(0, handleConnectTimeout);
86+
});
87+
}
7988
}
8089

8190
this._protocol.handshake(cb);
@@ -191,6 +200,20 @@ Connection.prototype.format = function(sql, values) {
191200
return SqlString.format(sql, values, this.config.stringifyObjects, this.config.timezone);
192201
};
193202

203+
Connection.prototype._handleConnectTimeout = function() {
204+
if (this._socket) {
205+
this._socket.setTimeout(0);
206+
this._socket.destroy();
207+
}
208+
209+
var err = new Error('connect ETIMEDOUT');
210+
err.errorno = 'ETIMEDOUT';
211+
err.code = 'ETIMEDOUT';
212+
err.syscall = 'connect';
213+
214+
this._handleNetworkError(err);
215+
};
216+
194217
Connection.prototype._handleNetworkError = function(err) {
195218
this._protocol.handleNetworkError(err);
196219
};

lib/ConnectionConfig.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function ConnectionConfig(options) {
1515
this.user = options.user || undefined;
1616
this.password = options.password || undefined;
1717
this.database = options.database;
18+
this.connectTimeout = options.connectTimeout || undefined;
1819
this.insecureAuth = options.insecureAuth || false;
1920
this.supportBigNumbers = options.supportBigNumbers || false;
2021
this.bigNumberStrings = options.bigNumberStrings || false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var common = require('../../common');
2+
var connection = common.createConnection({connectTimeout: 1000});
3+
var assert = require('assert');
4+
5+
connection.connect();
6+
7+
var connectErr;
8+
var rows = undefined;
9+
var fields = undefined;
10+
connection.query('SELECT SLEEP(3)', function(err, _rows, _fields) {
11+
connectErr = err;
12+
rows = _rows;
13+
fields = _fields;
14+
});
15+
16+
connection.end();
17+
18+
process.on('exit', function() {
19+
assert.ifError(connectErr);
20+
assert.deepEqual(rows, [{'SLEEP(3)': 0}]);
21+
assert.equal(fields[0].name, 'SLEEP(3)');
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var common = require('../../common');
2+
var connection = common.createConnection({host: '1.1.1.1', port: common.fakeServerPort, connectTimeout: 500});
3+
var assert = require('assert');
4+
5+
var testTimeout = setTimeout(function() {
6+
connection.destroy();
7+
}, 5000);
8+
9+
var connectErr;
10+
connection.connect(function(err) {
11+
connectErr = err;
12+
clearTimeout(testTimeout);
13+
});
14+
15+
process.on('exit', function() {
16+
assert.ok(connectErr);
17+
assert.equal(connectErr.code, 'ETIMEDOUT');
18+
assert.equal(connectErr.syscall, 'connect');
19+
assert.equal(connectErr.fatal, true);
20+
});

0 commit comments

Comments
 (0)