非常简单的Node.js客户端在许多http请求之后抛出错误ENOBUFS [英] Very simple Node.js client throws error ENOBUFS after many http requests

查看:897
本文介绍了非常简单的Node.js客户端在许多http请求之后抛出错误ENOBUFS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下设置:

node.js客户端向node.js服务器发出端到端请求。不到一分钟后,客户端失败并显示错误ENOBUFS。

A node.js client makes end-to-end requests to a node.js server. After less than a minute, the client fails with error ENOBUFS.

客户:

(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);   
    })()

服务器:

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');

虽然这个问题是类似的,我发布这个作为原始问题的概括(我使用的是post而不是get),带有测试用例。

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.

推荐答案

该问题似乎是Node.js HTTP客户端连接池的问题。

The issue appears to be a problem with the Node.js HTTP client connection pool.

如果将选项 agent:false 添加到选项参数中.org / docs / latest / api / http.html#http_http_request_options_callbackrel =nofollow> http.request() function 它将禁用连接池和让每个请求使用标题连接:关闭。此更改似乎允许客户端代码无限期运行。

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. This change seems to allow the client code to run indefinitely.

var options = {agent:false, host:"localhost", port:1337, /*...*/ };

执行此操作会降低HTTP客户端的性能,您应该会在客户端进程中看到频繁的暂停(大概是在V8运行时进行垃圾收集时)。但它似乎解决了你的问题!

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). But it does seem to solve your problem!

Per @joshp的评论,看看这个问题是否已在更高版本的Node.js中解决或考虑提交错误报告。

Per @joshp's comment, see if this issue has been addressed in a later version of Node.js or consider filing a bug report.

这篇关于非常简单的Node.js客户端在许多http请求之后抛出错误ENOBUFS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆