Node.js连接EMFILE-如何重用连接? [英] Nodejs connect EMFILE - How to reuse connections?
问题描述
我正在实现基于NodeJS的脚本,用于与Couchbase和其他服务进行通信.这是一个运行时间很长的脚本,过了一会儿我得到该服务的"connect EMFILE".我的代码示例如下:
I am implementing NodeJS based scripts for communicating to couchbase and another service. It is a long running script and after a while I get "connect EMFILE" for the service. My code sample is given below:
function createContainer(chunkName,recordingID,chunkData)
{
var swiftHTTPPath='http://'+swiftIPAddr+'/swift/v1/'+recordingID;
var path = '/swift/v1/'+recordingID;
var swiftOptions = {
hostname : swiftIPAddr,
port : swiftPort,
path : path,
method : 'PUT',
};
http.get(swiftHTTPPath, function(res) {
if(res.statusCode == '404')
{
var req = http.request(swiftOptions, function(res)
{
res.setEncoding('utf8');
res.on('data', function (chunk)
{
console.log('Container created');
});
res.on('end', function() {
});
});
req.on('error', function(e)
{
console.log(e.message);
});
req.end();
}
else
{
console.log('Container already exists');
}
}).on('error', function(e)
{
console.log('e.message);
});
}
以下命令显示有超过1024个打开的连接
The following command shows there are more than 1024 open connections
lsof -i -n -P > mylog.log
........
node 14135 ubuntu 1020u IPv4 5249347 0t0 TCP 10.1.1.1:53623->10.1.1.2:8091 (ESTABLISHED)
node 14135 ubuntu 1021u IPv4 5249350 0t0 TCP 10.1.1.1:42021->10.1.1.2:11210 (ESTABLISHED)
node 14135 ubuntu 1022u IPv4 5249351 0t0 TCP 10.1.1.1:53627->10.1.1.2:8091 (ESTABLISHED)
........
我可以增加ulimit值,但是我想知道我是否以正确的方式执行HTTP GET,POST请求.有没有办法重用/终止连接?
I can increase ulimit values but I would like to know am I doing the HTTP GET,POST request in the correct way. Is there a way to reuse/ terminate the connection ?
推荐答案
由于看起来所有(或大部分)请求都发送到单个主机,因此您可以将每个主机的并发套接字的最大数量设置为有限数量,例如100.使用Node的http
模块中的 agent.maxSockets
选项执行此操作
Since it looks like all (or most) of your requests go to a single host, you can set the maximum number of concurrent sockets per host to a limited number, such as 100. Do this with the agent.maxSockets
option in Node's http
module.
关于简单地重用连接,这应该会自动发生.事实并非如此,这使我相信您正在同时发出许多请求...因此您也可以通过逐步发出请求来解决此问题.
Regarding simply reusing connections, this should happen automatically. The fact that it isn't leads me to believe you are issuing many requests concurrently...so you could also solve this problem by issuing requests more gradually.
这篇关于Node.js连接EMFILE-如何重用连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!