Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node https request ECONRESET after 5 minutes

Tags:

node.js

https

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

like image 676
barsju Avatar asked Feb 15 '26 04:02

barsju


1 Answers

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.

like image 154
barsju Avatar answered Feb 16 '26 16:02

barsju