-
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
Conversation
lgtm 👍 |
👍 |
Hey guys, I do need to change one thing on this PR, though. Right now a timeout event will get emitted and sent down to a protocol error, but the socket itself will stay there still working on getting connected; I need to change it to destroy the socket when the timeout occurs still. |
|
||
this._socket.setTimeout(this.config.connectTimeout, handleConnectTimeout); | ||
this._socket.once('connect', function() { | ||
this.setTimeout(0, handleConnectTimeout); |
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 as this.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 (this.config.connectTimeout) {
var handleConnectTimeout = this._handleConnectTimeout.bind(this);
this._socket.setTimeout(this.config.connectTimeout);
this._socket.once('timeout', handleConnectTimeout);
this._socket.once('connect', function() {
this.setTimeout(0);
this.removeListener('timeout', handleConnectTimeout);
});
}
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 called
var net = require('net');
var s = net.connect(80, 'stackoverflow.com');
s.setTimeout(1000, function() { console.log('timeout'); });
s.on('connect', function() {
console.log('connected!');
s.setTimeout(0);
});
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.
Correct, because when you run a.setTimeout(0)
the socket will never emit a timeout
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:
var net = require('net');
var s = net.connect(80, 'stackoverflow.com');
s.setTimeout(1000, function() { console.log('timeout'); });
s.on('connect', function() {
console.log('connected!');
s.setTimeout(0);
setTimeout(function(){
// Sometime later some user code does
s.setTimeout(2);
// Which didn't do anything before, or maybe they are adding their own
// timeout listener for some reason
// Our timeout listener is now triggered since we never removed it
},100);
});
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
@dougwilson could you add a line to changelog as well please? |
@sidorares done :) |
Interesting, domains test fail on node 0.8
|
I re-ran the tests on Travis and it passed now. That test is flawed, because it does not check |
Great, merging |
Add option for connect timeout
@sidorares I also just sent PR #728 to re-throw those errors so if an error occurs at points in the domains test, we can see what happened on Travis CI. |
💖 thx for all the PRs @dougwilson ! |
@dougwilson also just gave you commit bits / npm push access. Enjoy! |
Thanks, @felixge It shall not be abused! |
This adds a new option
connectTimeout
which specifies a timeout (in milliseconds) for the initial connection to timeout. This applies to the time between sending data to the remote server and establishing a TCP connection. The default value is to not have a timeout, which makes it backwards-compatible.This timeout is named
connectTimeout
because it specifically ONLY applies to the connection establishment, nothing else. This means that the timeout will no longer time out any operation after the connection has been successfully established.Fixes #708
Fixes #717