Skip to content

Commit b865ad2

Browse files
committed
added connect timeout event and tests
1 parent d30c40a commit b865ad2

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

lib/connection.js

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var net = require('net');
1+
var Net = require('net');
22
var util = require('util');
3-
var tls = require('tls');
3+
var Tls = require('tls');
44
var EventEmitter = require('events').EventEmitter;
55
var Queue = require('double-ended-queue');
66

@@ -30,9 +30,9 @@ function Connection (opts)
3030

3131
if (!opts.config.stream) {
3232
if (opts.config.socketPath) {
33-
this.stream = net.connect(opts.config.socketPath);
33+
this.stream = Net.connect(opts.config.socketPath);
3434
} else {
35-
this.stream = net.connect(opts.config.port, opts.config.host);
35+
this.stream = Net.connect(opts.config.port, opts.config.host);
3636
}
3737
} else {
3838
// if stream is a function, treat it as "stream agent / factory"
@@ -74,19 +74,17 @@ function Connection (opts)
7474
this.threadId = null;
7575
this._handshakePacket = null;
7676
this._fatalError = null;
77-
77+
this._protocolError = null;
7878
this._outOfOrderPackets = [];
7979

80-
this.stream.once('error', function (err) {
81-
connection._handleNetworkError(err);
82-
});
80+
this.stream.once('error', connection._handleNetworkError.bind(this));
8381

8482
// see https://gist.github.com/khoomeister/4985691#use-that-instead-of-bind
8583
this.packetParser = new PacketParser(function (p) { connection.handlePacket(p); });
84+
8685
this.stream.on('data', function (data) {
8786
connection.packetParser.execute(data);
8887
});
89-
this._protocolError = null;
9088
this.stream.on('end', function () {
9189
// we need to set this flag everywhere where we want connection to close
9290
if (connection._closing) {
@@ -119,6 +117,14 @@ function Connection (opts)
119117
// most common example: "Too many connections" error ( packet is sent immediately on connection attempt, we don't know server encoding yet)
120118
// will be overwrittedn with actial encoding value as soon as server handshake packet is received
121119
this.serverEncoding = 'utf8';
120+
121+
if (this.config.connectTimeout) {
122+
var timeoutHandler = this._handleTimeoutError.bind(this);
123+
this.stream.setTimeout(this.config.connectTimeout, timeoutHandler);
124+
this.stream.once('connect', function () {
125+
connection.stream.setTimeout(0, timeoutHandler);
126+
});
127+
}
122128
}
123129
util.inherits(Connection, EventEmitter);
124130

@@ -139,6 +145,20 @@ Connection.prototype._handleNetworkError = function (err) {
139145
connection._fatalError = err;
140146
};
141147

148+
Connection.prototype._handleTimeoutError = function () {
149+
if (this.stream) {
150+
this.stream.setTimeout(0);
151+
this.stream.destroy();
152+
}
153+
154+
var err = new Error('connect ETIMEDOUT');
155+
err.errorno = 'ETIMEDOUT';
156+
err.code = 'ETIMEDOUT';
157+
err.syscall = 'connect';
158+
159+
this._handleNetworkError(err);
160+
};
161+
142162
// notify all commands in the queue and bubble error as connection "error"
143163
// called on stream error or unexpected termination
144164
Connection.prototype._notifyError = function (err) {
@@ -191,15 +211,15 @@ Connection.prototype.writePacket = function (packet) {
191211
this.write(packet.buffer);
192212
};
193213

194-
if (tls.TLSSocket) {
214+
if (Tls.TLSSocket) {
195215
// 0.11+ environment
196216
Connection.prototype.startTLS = function _startTLS (onSecure) {
197217
if (this.config.debug) {
198218
console.log('Upgrading connection to TLS');
199219
}
200220
var connection = this;
201221
var stream = this.stream;
202-
var secureContext = tls.createSecureContext({
222+
var secureContext = Tls.createSecureContext({
203223
ca : this.config.ssl.ca,
204224
cert : this.config.ssl.cert,
205225
ciphers : this.config.ssl.ciphers,
@@ -209,7 +229,7 @@ if (tls.TLSSocket) {
209229

210230
var rejectUnauthorized = this.config.ssl.rejectUnauthorized;
211231
var secureEstablished = false;
212-
var secureSocket = new tls.TLSSocket(connection.stream, {
232+
var secureSocket = new Tls.TLSSocket(connection.stream, {
213233
rejectUnauthorized : rejectUnauthorized,
214234
requestCert : true,
215235
secureContext : secureContext,
@@ -244,7 +264,6 @@ if (tls.TLSSocket) {
244264
console.log('Upgrading connection to TLS');
245265
}
246266
var connection = this;
247-
var tls = require('tls');
248267
var crypto = require('crypto');
249268
var config = this.config;
250269
var stream = this.stream;
@@ -256,7 +275,7 @@ if (tls.TLSSocket) {
256275
ca : config.ssl.ca,
257276
ciphers : config.ssl.ciphers
258277
});
259-
var securePair = tls.createSecurePair(credentials, false, true, rejectUnauthorized);
278+
var securePair = Tls.createSecurePair(credentials, false, true, rejectUnauthorized);
260279

261280
if (stream.ondata) {
262281
stream.ondata = null;
@@ -278,7 +297,7 @@ if (tls.TLSSocket) {
278297

279298
Connection.prototype.pipe = function () {
280299
var connection = this;
281-
if (this.stream instanceof net.Stream) {
300+
if (this.stream instanceof Net.Stream) {
282301
this.stream.ondata = function (data, start, end) {
283302
connection.packetParser.execute(data, start, end);
284303
};

test/common.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ module.exports.createConnection = function (args, callback) {
5959
}
6060

6161
var params = {
62-
host: config.host,
62+
host: args.host || config.host,
6363
rowsAsArray: args.rowsAsArray,
6464
user: (args && args.user) || config.user,
6565
password: (args && args.password) || config.password,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var common = require('../../common');
2+
var connection = common.createConnection({
3+
host: 'www.google.com'
4+
});
5+
var assert = require('assert');
6+
7+
var errorCount = 0;
8+
var error = null;
9+
10+
11+
connection.on('error', function (err) {
12+
errorCount++;
13+
error = err;
14+
});
15+
16+
process.on('exit', function () {
17+
assert.equal(errorCount, 1);
18+
assert.equal(error.code, 'ETIMEDOUT');
19+
});

0 commit comments

Comments
 (0)