admin管理员组文章数量:1026073
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
2 Answers
Reset to default 4I 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
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
2 Answers
Reset to default 4I 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
本文标签: javascriptNodejsServer close KeepAlive connection in middle of requestStack Overflow
版权声明:本文标题:javascript - Node.JS, Server close Keep-Alive connection in middle of request - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744263785a2088675.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论