Node.js 多次http.get请求到本地主机后,EADDRNOTAVAIL无效

Node.js 多次http.get请求到本地主机后,EADDRNOTAVAIL无效,node.js,httprequest,Node.js,Httprequest,在28233个请求之后对本地主机(Apache)执行许多http.get-requests时,我得到了EADDRNOTAVAIL 损坏时: 我无法向本地主机发出http.request(大约)10秒(EADDRNOTAVAIL) 而这10秒 我能做卷发http://localhost(Apache中没有错误,它仍然像charm一样工作) 我可以执行http.get-request(从node.js)到www.google.com(该错误仅影响对localhost的请求) 10秒后 我可

在28233个请求之后对本地主机(Apache)执行许多http.get-requests时,我得到了
EADDRNOTAVAIL

损坏时:

  • 无法向本地主机发出http.request(大约)10秒(
    EADDRNOTAVAIL
而这10秒

  • 我能做卷发http://localhost(Apache中没有错误,它仍然像charm一样工作)
  • 可以执行http.get-request(从node.js)到
    www.google.com
    (该错误仅影响对localhost的请求)
10秒后

  • 可以再次向localhost发出http.request(就好像node.js已经自我修复一样)
代码如下:

var http = require( "http");

function httpRequest( callback ) {
    var options = {
            host: 'localhost',
            port: 80,
            path: ''
        },
        data = "";

    http.get(options, function(resp){
        resp.on('data', function( chunk ){   
            data += chunk;
        }).on("end", function() {
            callback( null, data );
        });
    }).on("error", function(e){
            callback( e );
    });
}

function loop( i, callback ) {
    if( i < 100000 ) {
        httpRequest( function( err, data ) {
            if( err ) {
                console.log( "Error!", i, err );
                return;
            }
            if( i % 1000 === 0 ) {
                console.log( i );
            }
            loop( i+1, callback );
        });
    } else {
        callback();
    }
}

console.log( "GO!");
loop( 0, function() {
   console.log( "READY!");
});
var http=require(“http”);
函数httpRequest(回调){
变量选项={
主机:“localhost”,
港口:80,
路径:“”
},
数据=”;
http.get(选项、函数(resp){
响应('data',函数(块){
数据+=块;
}).on(“结束”,函数(){
回调(空,数据);
});
}).on(“错误”,函数(e){
撤回(e);
});
}
函数循环(i,回调){
如果(i<100000){
httpRequest(函数(错误,数据){
如果(错误){
log(“Error!”,i,err);
返回;
}
如果(i%1000==0){
控制台日志(i);
}
循环(i+1,回调);
});
}否则{
回调();
}
}
console.log(“开始!”);
循环(0,函数(){
console.log(“准备就绪!”);
});

我通过覆盖默认全局代理找到了解决方案。一种可能性是设置
maxSockets:1

var http = require( "http"),
    agent = new http.Agent( {maxSockets: 1} ); // <-- this is new

function httpRequest( callback ) {
    var options = {
            host: 'localhost',
            port: 80,
            path: '',
            agent: agent // <-- and this is new
        },
...

我也在上发布了这个问题。

MacOS解决方案,或者至少是对我有效的解决方案


并将请求发送到mac.local

谢谢。将代理设置为false可以解决我在AWS SDK和DynamoDB连接中遇到的一个相关问题。(在某些情况下,请求周期性地花费25秒而不是50秒,并且EADDRNOTAVAIL偶尔抛出。这段时间可能对应于28233个请求)即使我也面临同样的问题。谢谢,我节省了很多时间。。。干杯
var http = require( "http");

function httpRequest( callback ) {
    var options = {
            host: 'localhost',
            port: 80,
            path: '',
            agent: false // <-- here
        },
...