node.js http.get在向远程站点发送5个请求后挂起 [英] node.js http.get hangs after 5 requests to remote site

查看:118
本文介绍了node.js http.get在向远程站点发送5个请求后挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个简单的api端点,以确定我的服务器是否能够访问互联网。它的效果很好,但在5个请求(正好5次,每次)之后,请求挂起。当我将Google切换到Hotmail.com时,会发生同样的事情,这使我认为这是我的结局。我需要关闭http.get请求吗?我的印象是这个函数会自动关闭请求。

  //可能是一个糟糕的假设,但如果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中, maxSockets 设置为

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.

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

Here's the reason of the "exactly 5": https://nodejs.org/docs/v0.10.36/api/http.html#http_agent_maxsockets

Internally, the 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.

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:

http.get("http://www.google.com", function(r){
  r.on('data', function() { /* do nothing */ });
  ...
});

If you run into the problem that your /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 }, ...)

Or perhaps use a HEAD request instead of GET.

(PS: in case http.get generates an error, you should still end the HTTP response by using res.end() or something like that).

NOTE: in Node.js versions >= 0.11, maxSockets is set to Infinity.

这篇关于node.js http.get在向远程站点发送5个请求后挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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