barsju
barsju

Reputation: 4446

Node https request ECONRESET after 5 minutes

Im trying to make https request to a server that takes more than 5 minutes to respond. ~7 mins before any data is transmitted over the socket and about 11 mins for the request to complete. The request works fine when using Curl, but when making the request using node.js I get this error:

Error:  { Error: read ECONNRESET
    at exports._errnoException (util.js:1026:11)
    at TLSWrap.onread (net.js:564:26) code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

The code used to make the request:

const https = require('https');

https.get({
    hostname: 'xxx',
    path: 'xxx',
    auth: 'xxx'
  },
  (res) => {
    res.on('data', (d) => {
      process.stdout.write(d);
    });
  }).on('error', (e) => {
  console.error(e);
});

Since the request fails after exactly 5 mins (300 seconds) I'm guessing there is some sort of timeout, but I cannot seem to find out which one or where it is. It might also be a server side timeout, but then it is strange that it works with Curl..

Upvotes: 3

Views: 3047

Answers (2)

barsju
barsju

Reputation: 4446

The solution was to use TCP Keep-Alive. https://nodejs.org/api/net.html#net_socket_setkeepalive_enable_initialdelay

req.on('socket', (s) => {
  s.setKeepAlive(true, 240000);
});

This will send a TCP ACK packets on the socket every 4 minutes, which was enough to solve my problem.

Upvotes: 4

mikep
mikep

Reputation: 3905

From Node js ECONNRESET ,

"ECONNRESET" means the other side of the TCP conversation abruptly closed its end of the connection.

From What option causes curl to reconnect infinite times? ,

curl will try to reconnect every time the requested operation will timeout.

This is elaborated as:

I mean using libcurl as a 3rd party software package

Hope this helps.

Upvotes: 0

Related Questions