node.js http.get在向远程站点发送5个请求后挂起 [英] node.js http.get hangs after 5 requests to remote site
问题描述
//可能是一个糟糕的假设,但如果Google无法访问通常安全地说,服务器无法访问互联网
//在仪表板中使用此客户端启用/禁用Internet资源
app.get('/ api / internetcheck' ,function(req,res){
console.log(尝试google ...);
http.get(http://www.google.com,function(r){
console.log(Got status code !:+ r.statusCode.toString());
res.send(r.statusCode.toString());
res.end );
console.log(ended!);
})on('error',function(e){
console.log(Got error:+ e。消息);
});
});
这是em的原因5 : https://nodejs.org/docs/v0 .10.36 / api / http.html#http_agent_maxsockets
在内部, http
模块使用代理类来管理HTTP请求。默认情况下,该代理将允许最多5个打开的连接到同一个HTTP服务器。
在代码中,不消耗由谷歌。因此,代理假定您没有完成请求,并将保持连接的打开状态。因此,在5个请求之后,代理将不允许您再次创建新的连接,并将开始等待任何现有的连接完成。
明显的解决方案将只是消费数据:
http.get(http://www.google.com,function r){
r.on('data',function(){/ * do nothing * /});
...
});
如果您遇到问题,您的 / api / internetcheck
路由被称为很多,所以你需要允许超过5个并发连接,你可以提高连接池大小,或者只是完全禁用代理(尽管你仍然需要在两种情况下使用数据);
//增加池大小
http.globalAgent.maxSockets = 100;
//禁用代理
http.get({hostname:'www.google.com',路径:'/',代理:false},...)
或者可以使用 HEAD
请求而不是$ $ c $
c> GET 。
(PS:如果 http.get
生成错误,您仍然应该使用 res.end()
或类似的东西来结束HTTP响应。
注意:在Node.js版本> = 0.11中, I'm writing a simple api endpoint to determine if my server is able to reach the internet. It works great, but after 5 requests (exactly 5, every time) the request hangs. Same thing happens when I switch Google to Hotmail.com, which makes me think that this is something on my end. Do I need to close the http.get requests? I was under the impression that this function closes the requests automatically.
Here's the reason of the "exactly 5": https://nodejs.org/docs/v0.10.36/api/http.html#http_agent_maxsockets Internally, the In your code, you don't consume the actual response sent by Google. So the agent assumes that you're not done with the request, and will keep the connection open. And so after 5 requests, the agent won't allow you to create a new connection anymore and will start waiting for any of the existing connections to complete. The obvious solution would be to just consume the data: If you run into the problem that your Or perhaps use a (PS: in case NOTE: in Node.js versions >= 0.11, 这篇关于node.js http.get在向远程站点发送5个请求后挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! maxSockets
设置为
。// probably a poor assumption, but if Google is unreachable its generally safe to say that the server can't access the internet
// using this client side in the dashboard to enable/disable internet resources
app.get('/api/internetcheck', function(req, res) {
console.log("trying google...");
http.get("http://www.google.com", function(r){
console.log("Got status code!: " +r.statusCode.toString());
res.send(r.statusCode.toString());
res.end();
console.log("ended!");
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
});
http
module uses an agent class to manage HTTP requests. That agent will, by default, allow for a maximum of 5 open connections to the same HTTP server.http.get("http://www.google.com", function(r){
r.on('data', function() { /* do nothing */ });
...
});
/api/internetcheck
route is called a lot, so you need to allow for more than 5 concurrent connections, you can either up the connection pool size, or just disable the agent completely (although you would still need to consume the data in both cases);// increase pool size
http.globalAgent.maxSockets = 100;
// disable agent
http.get({ hostname : 'www.google.com', path : '/', agent : false }, ...)
HEAD
request instead of GET
.http.get
generates an error, you should still end the HTTP response by using res.end()
or something like that).maxSockets
is set to Infinity
.