request.socket.setTimeout() timeout event closes connection contrary to the documentation

1,910 views
Skip to first unread message

min

unread,
Jan 23, 2012, 4:38:21 PM1/23/12
to nodejs
Hi,

I'm trying to check idle client connections from the http server.
When I use request.socket.setTimeout(10000), the timeout event closes
connection contrary to the documentation.

The node.js documentation says for socket.setTimeout(),
Event: 'timeout'
function () { }
Emitted if the socket times out from inactivity. This is only to
notify that the socket has been idle. The user must manually close the
connection.

Here is the code:
function onRequest(request, response) {
request.socket.setTimeout(10000);
request.socket.addListener("timeout", function() {
console.log("socket timeout");
});
http.createServer(onRequest).listen(port);

Is this a bug in the current node.js or am I doing something wrong?
I tried v0.6.1-pre and the latest v0.6.8. Both showed the same
behavior.

Min


mscdex

unread,
Jan 23, 2012, 5:50:21 PM1/23/12
to nodejs
On Jan 23, 4:38 pm, min <m...@navcomtech.com> wrote:
> Is this a bug in the current node.js or am I doing something wrong?
> I tried v0.6.1-pre and the latest v0.6.8.  Both showed the same
> behavior.

The documentation for sockets ('net' module) is correct. However, http/
https servers set their own socket timeout which calls
socket.destroy(): https://github.com/joyent/node/blob/v0.6/lib/http.js#L1389-L1391

What you'd need to do is:
function onRequest(request, response) {
request.socket.removeAllListeners('timeout');
request.socket.setTimeout(10000);
request.socket.on('timeout', function() {

min

unread,
Jan 23, 2012, 6:52:36 PM1/23/12
to nodejs
Thanks!

It works.

Min
Reply all
Reply to author
Forward
0 new messages