EADDRNOTAVAIL经过许多http.get请求到localhost

在28233请求之后,对localhost(Apache)执行了很多http.get-请求时,我得到了EADDRNOTAVAIL

破碎时:

  • 不能做一个http.request本地主机(约)10秒( EADDRNOTAVAIL

这10秒钟

  • 可以做 curl http://localhost (Apache中没有错误,它仍然像魅力一样工作)
  • 可以做一个http.get-请求(从node.js)到www.google.com (该错误只影响到本地主机的请求)

10秒后

  • 可以再次对本地主机执行 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!"); }); 

我已经find了覆盖默认全局代理的解决scheme。 一种可能性是设置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 }, ... 

有了这个更正上面的例子工作。 但是我的生产代码中仍然存在EADDRNOTAVAIL问题 ,因此最终将agent设置为false

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

我已经在GitHub上发布了这个问题。