Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a timeout for client http connections in node.js

Tags:

node.js

I'm writing a node.js application that needs to talk to a server. It establishes an http connection with the following code:

var client = http.createClient(u.port, u.hostname, u.secure);
client.on("error", function(exception) {
    logger.error("error from client");
});
var request = client.request(method, u.path, headers);

I don't see any option in the node.js documentation for setting a timeout on the connection, and it seems to be set to 20 seconds by default. The problem I'm having is that I have users in China on what appears to be a slow or flaky network, who sometimes hit the timeout connecting to our datacenter in the US. I'd like to increase the timeout to 1 minute, to see if that fixes it for them.

Is there a way to do that in node.js?

like image 227
Dan List Avatar asked Aug 27 '10 17:08

Dan List


People also ask

How would you set a timeout on an HTTP request?

Stanza entries for timeout settings are usually located in the [server] stanza of the WebSEAL configuration file. After the initial connection handshake has occurred, this stanza entry specifies how long WebSEAL holds the connection open for the initial HTTP or HTTPS request. The default value is 120 seconds.

What is the default timeout for HTTP request?

The default value is 100,000 milliseconds (100 seconds).

What is HTTP connect timeout?

The HyperText Transfer Protocol (HTTP) 408 Request Timeout response status code means that the server would like to shut down this unused connection. It is sent on an idle connection by some servers, even without any previous request by the client.

Can you use setTimeout in node?

The setTimeout function is used to call a function after the specified number of milliseconds. The delay of the called function begins after the remaining statements in the script have finished executing. The setTimeout function is found in the Timers module of Node. js.


3 Answers

You have to wait for the client socket connection to be established first, before setting the timeout. To do this, add a callback for the 'socket' event:

req.on('socket', function (socket) {
    myTimeout = 500; // millis
    socket.setTimeout(myTimeout);  
    socket.on('timeout', function() {
        console.log("Timeout, aborting request")
        req.abort();
    });
}).on('error', function(e) {
    console.log("Got error: " + e.message);
    // error callback will receive a "socket hang up" on timeout
});

See this answer.

like image 198
Pavel Avatar answered Oct 13 '22 00:10

Pavel


Try

request.socket.setTimeout(60000); // 60 sec

like image 21
Matjaz Lipus Avatar answered Oct 12 '22 22:10

Matjaz Lipus


There is no capability in Node to increase connect timeout. Since usually connect timeout (i.e. connection establishing timeout) is OS-wide setting for all applications (e.g., 21 seconds in Windows, from 20 to 120 seconds in Linux). See also Timouts in Request package.

In contrast, Node allows to set decreased timeout and abort connecting even in case when the connection is not yet established.

The further timeouts (in case of connection has been established) can be controlled according to the documentation (see request.setTimeout, socket.setTimeout).

like image 39
ruvim Avatar answered Oct 12 '22 23:10

ruvim