Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default timeout for NPM request module (REST client)?

Following will be my node.js call to retrive some data, which is taking more than 1 minute. Here this will be timeout at 1 minute (60 seconds). I put a console log for the latency also. However I have configured the timeout for 120 seconds but it is not reflecting. I know the default level nodejs server timeout is 120 seconds but still I get the timeout (of 60 seconds) from this request module for this call. Please provide your insights on this.

var options = {
  method: 'post',
  url:url,
  timeout: 120000,
  json: true,
  headers: {
    "Content-Type": "application/json",
    "X-Authorization": "abc",
    "Accept-Encoding":"gzip"
  }
}
var startTime = new Date();
request(options, function(e, r, body) {
  var endTime = new Date();
  var latencyTime = endTime - startTime;
  console.log("Ended. latencyTime:"+latencyTime/1000);
  res.status(200).send(body);

});
like image 375
Amila Iddamalgoda Avatar asked Sep 27 '16 14:09

Amila Iddamalgoda


1 Answers

From the request options docs, scrolling down to the timeout entry:

timeout - Integer containing the number of milliseconds to wait for a server to send response headers (and start the response body) before aborting the request. Note that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option (the default in Linux can be anywhere from 20-120 seconds).

Note the last part "if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option".

There is also an entire section on Timeouts. Based on that, and your code sample, we can modify the request sample as such

request(options, function(e, r, body) {
  if (e.code === 'ETIMEDOUT' && e.connect === true){
  // when there's a timeout and connect is true, we're meeting the
  // conditions described for the timeout option where the OS governs
  console.log('bummer');
  }
});

If this is true, you'll need to decide if changing OS settings is possible and acceptable (this is beyond the scope of this answer and such a question would be better on Server Fault).

like image 66
Matthew Bakaitis Avatar answered Oct 13 '22 09:10

Matthew Bakaitis