-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Add option for connect timeout #726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -76,6 +76,15 @@ Connection.prototype.connect = function(cb) { | |
this._protocol.on('unhandledError', this._handleProtocolError.bind(this)); | ||
this._protocol.on('drain', this._handleProtocolDrain.bind(this)); | ||
this._protocol.on('end', this._handleProtocolEnd.bind(this)); | ||
|
||
if (this.config.connectTimeout) { | ||
var handleConnectTimeout = this._handleConnectTimeout.bind(this); | ||
|
||
this._socket.setTimeout(this.config.connectTimeout, handleConnectTimeout); | ||
this._socket.once('connect', function() { | ||
this.setTimeout(0, handleConnectTimeout); | ||
}); | ||
} | ||
} | ||
|
||
this._protocol.handshake(cb); | ||
|
@@ -191,6 +200,20 @@ Connection.prototype.format = function(sql, values) { | |
return SqlString.format(sql, values, this.config.stringifyObjects, this.config.timezone); | ||
}; | ||
|
||
Connection.prototype._handleConnectTimeout = function() { | ||
if (this._socket) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I actually just pushed the change, so you can see it now :) |
||
this._socket.setTimeout(0); | ||
this._socket.destroy(); | ||
} | ||
|
||
var err = new Error('connect ETIMEDOUT'); | ||
err.errorno = 'ETIMEDOUT'; | ||
err.code = 'ETIMEDOUT'; | ||
err.syscall = 'connect'; | ||
|
||
this._handleNetworkError(err); | ||
}; | ||
|
||
Connection.prototype._handleNetworkError = function(err) { | ||
this._protocol.handleNetworkError(err); | ||
}; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var common = require('../../common'); | ||
var connection = common.createConnection({connectTimeout: 1000}); | ||
var assert = require('assert'); | ||
|
||
connection.connect(); | ||
|
||
var connectErr; | ||
var rows = undefined; | ||
var fields = undefined; | ||
connection.query('SELECT SLEEP(3)', function(err, _rows, _fields) { | ||
connectErr = err; | ||
rows = _rows; | ||
fields = _fields; | ||
}); | ||
|
||
connection.end(); | ||
|
||
process.on('exit', function() { | ||
assert.ifError(connectErr); | ||
assert.deepEqual(rows, [{'SLEEP(3)': 0}]); | ||
assert.equal(fields[0].name, 'SLEEP(3)'); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var common = require('../../common'); | ||
var connection = common.createConnection({host: '1.1.1.1', port: common.fakeServerPort, connectTimeout: 500}); | ||
var assert = require('assert'); | ||
|
||
var testTimeout = setTimeout(function() { | ||
connection.destroy(); | ||
}, 5000); | ||
|
||
var connectErr; | ||
connection.connect(function(err) { | ||
connectErr = err; | ||
clearTimeout(testTimeout); | ||
}); | ||
|
||
process.on('exit', function() { | ||
assert.ok(connectErr); | ||
assert.equal(connectErr.code, 'ETIMEDOUT'); | ||
assert.equal(connectErr.syscall, 'connect'); | ||
assert.equal(connectErr.fatal, true); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need to pass handler as argument here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is needed to remove the specific
timeout
listener.this.setTimeout(0, handleConnectTimeout);
is the same asthis.setTimeout(0); this.removeListener('timeout', handleConnectTimeout)
. The timeout listener needs to be removed so it does not trigger after we have established a connection, since this is just a timeout for connecting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This
if
block could also be written as:if that makes it clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just checked
setTimeout(0)
without handler and previous timer is not calledThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, because when you run
a.setTimeout(0)
the socket will never emit atimeout
event any more, because the timeout timers are unenrolled. But if some user-land code is setting a timeout on our socket, our timeout listener will then suddenly trigger. We need to remove our listener to be safe. Try:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am just trying to make sure that this timeout function is only triggered for a timeout during the connection establishment and not at any possible later point, which is why I went through extra effort to remove the timeout listener we added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point