$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Node.JS, Server close Keep-Alive connection in middle of request - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Node.JS, Server close Keep-Alive connection in middle of request - Stack Overflow

matteradmin7PV0评论

I have Node.js HTTP server which closes ining connections if their idle for 10 seconds, and client which uses Keep-Alive connections pool to request this server. When I start send requests with interval 10 seconds, I got periodically error messages ECONNRESET, I think because connection closes in middle of HTTP request. Is there any elegant solution except simple resending request?

server.js

var server = require('http').createServer(function(req, res) {
  console.log(new Date(), "Request ", req.method);
  res.end("Nice");
});

server.timeout = 10000;

server.on("connection", function(socket) {
  console.log("New connection");
  socket._created = new Date().getTime();
  socket.on("close", function() {
    var now = new Date().getTime();
    console.log(new Date(), "Socket closed, TTL", (now - socket._created)/1000);
  });
});
server.listen(3000);

client.js

var http = require("http");

var agent = new http.Agent({
  keepAlive: true
});

var reqSent = 0;
var NEED_REQS = 10;
var TIMEOUT = 10000;

function _req() {
  var start = new Date().getTime();
  var req = http.get({
    host: "localhost",
    port: 3000,
    agent: agent
  }, function(res) {
    reqSent++;
    var now = new Date().getTime();
    console.log("Sent:", reqSent, (now - start)/1000);

    if(reqSent >= NEED_REQS) {
      agent.destroy();
      return;
    }
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
    });

    setTimeout(function() {
      _req();
    }, TIMEOUT);
  });
  req.on("error", function(err) {
    console.error(err);
  });
}

_req();

Running client.js

$ node client.js
Sent: 1 0.028
Sent: 2 0.008
Sent: 3 0.002
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }

I have Node.js HTTP server which closes ining connections if their idle for 10 seconds, and client which uses Keep-Alive connections pool to request this server. When I start send requests with interval 10 seconds, I got periodically error messages ECONNRESET, I think because connection closes in middle of HTTP request. Is there any elegant solution except simple resending request?

server.js

var server = require('http').createServer(function(req, res) {
  console.log(new Date(), "Request ", req.method);
  res.end("Nice");
});

server.timeout = 10000;

server.on("connection", function(socket) {
  console.log("New connection");
  socket._created = new Date().getTime();
  socket.on("close", function() {
    var now = new Date().getTime();
    console.log(new Date(), "Socket closed, TTL", (now - socket._created)/1000);
  });
});
server.listen(3000);

client.js

var http = require("http");

var agent = new http.Agent({
  keepAlive: true
});

var reqSent = 0;
var NEED_REQS = 10;
var TIMEOUT = 10000;

function _req() {
  var start = new Date().getTime();
  var req = http.get({
    host: "localhost",
    port: 3000,
    agent: agent
  }, function(res) {
    reqSent++;
    var now = new Date().getTime();
    console.log("Sent:", reqSent, (now - start)/1000);

    if(reqSent >= NEED_REQS) {
      agent.destroy();
      return;
    }
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
    });

    setTimeout(function() {
      _req();
    }, TIMEOUT);
  });
  req.on("error", function(err) {
    console.error(err);
  });
}

_req();

Running client.js

$ node client.js
Sent: 1 0.028
Sent: 2 0.008
Sent: 3 0.002
{ [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
Share Improve this question edited Apr 22, 2015 at 16:09 calibr asked Apr 21, 2015 at 22:44 calibrcalibr 591 gold badge2 silver badges10 bronze badges 5
  • What could the solution possibly be? You are closing the connection. The client alerts you that the connection was closed. What else is there? Re-send your request. – Brad Commented Apr 22, 2015 at 16:47
  • Why would you possibly have a server-side timeout that is set to a value shorter than some of your requests take. The server-side timeout should be set longer than any request will be expected to take to finish. – jfriend00 Commented Apr 22, 2015 at 22:54
  • @jfriend00 this is timeout between requests, each request takes short time to process. – calibr Commented Apr 24, 2015 at 16:45
  • possible duplicate of How to handle ECONNRESET, Connection reset by peer – Paul Sweatte Commented Jun 2, 2015 at 15:53
  • You might want to look into github./node-modules/agentkeepalive – saurabh Commented Jul 17, 2020 at 6:38
Add a ment  | 

2 Answers 2

Reset to default 4

I did reproduce your problem with sending requests each 5 seconds. The default keepAlive timeout in nodejs is 5 seconds, https://nodejs/api/http.html#http_server_keepalivetimeout

This github issue against nodejs explains the behavior well, https://github./nodejs/node/issues/20256 The workaround would be to set keepAlive timout client side to something less than the timeout on server side but I don't see such option in nodejs, https://nodejs/api/http.html#http_new_agent_options

I think that there is only one solution for this - resend request if ECONNRESET obtained, I found good lib which wraps such logic https://github./FGRibreau/node-request-retry

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far