17

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);

});
Amila Iddamalgoda
  • 4,166
  • 11
  • 46
  • 85

1 Answers1

15

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).

Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
  • Hi Matthew, I'm getting this error. { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }. Any thoughts why I'm getting this ?. Through a REST client (POSTMAN/ etc) I was able to get the response within 1 second. But through this node module it takes some time. And when I cat /proc/sys/net/ipv4/tcp_syn_retries I got the value of 6. – Amila Iddamalgoda Sep 28 '16 at 14:44
  • First, if `ECONNRESET` is the error then the timeout isn't the problem. You have a different issue and you should see this other Stack Overflow answer: http://stackoverflow.com/a/17637900/2247344 – Matthew Bakaitis Sep 28 '16 at 17:43
  • Is this answer still correct? When I try to use it, or any of the options, the e or error parameter never contains a code property or connect property. It does contain a message that says timeout – Trevor May 01 '18 at 20:39
  • I haven't used request in a few months, but the documentation is consistent with this answer. So I can't say for certain, but it seems the module is still built this way (or has changed but docs aren't updated?). – Matthew Bakaitis May 02 '18 at 00:01