[英]Very simple Node.js client throws error ENOBUFS after many http requests
I have the following set up: 我有以下设置:
A node.js client makes end-to-end requests to a node.js server. node.js客户端向node.js服务器发出端到端请求。 After less than a minute, the client fails with error ENOBUFS. 不到一分钟后,客户端失败并显示错误ENOBUFS。
client: 客户:
(function(){
var loadUrl=function(){
var http=require('http');
var querystring=require('querystring');
var options = {host:"localhost",port:1337,path:'/post',method:'POST'};
var req = http.request(options, function(res){
res.setEncoding('utf8');
var body='';
res.on('data', function (chunk) {
body+=chunk;
});
res.on('end', function (chunk) {
loadUrl();
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
var post_data = querystring.stringify({id:0});
req.write(post_data);
req.end();
}
setTimeout(loadUrl,1000);
})()
server: 服务器:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
While this question is similar, I am posting this as a generalization of the original question (I am using post rather than get), with a test case. 虽然这个问题很相似,但我将这个问题作为原始问题的概括(我使用的是post而不是get),并附带一个测试用例。
The issue appears to be a problem with the Node.js HTTP client connection pool. 该问题似乎是Node.js HTTP客户端连接池的问题。
If you add the option agent:false
to the options
argument of the http.request()
function it will disable connection pooling and have each request use the header Connection: close
. 如果将选项agent:false
添加到http.request()
函数的options
参数中,它将禁用连接池并让每个请求使用标题Connection: close
。 This change seems to allow the client code to run indefinitely. 此更改似乎允许客户端代码无限期运行。
var options = {agent:false, host:"localhost", port:1337, /*...*/ };
Doing this will degrade the performance of the HTTP clients and you should see frequent pauses in the client process (presumably while the V8 runtime does garbage collection). 这样做会降低HTTP客户端的性能,您应该会在客户端进程中看到频繁的暂停(可能是在V8运行时执行垃圾回收时)。 But it does seem to solve your problem! 但它似乎解决了你的问题!
Per @joshp's comment, see if this issue has been addressed in a later version of Node.js or consider filing a bug report. Per @joshp的评论,看看这个问题是否已在更高版本的Node.js中得到解决,或者考虑提交错误报告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.