我使用的是mongodb 2.2.3和nodejs 0.10.5。 我正在创建一个应用程序,我将连接到不同主机上的不同mongodb实例。 动态创建和重用不同连接的最有效方法是什么?
例如,我在数组hostArray
中有几个主机,我想要获取其中的所有集合。
function getCollectionNames(hostsArray) {
async.map(hostsArray,function(item,callback){
uri = "mongodb://" + item['user'] + ":" + item['passw'] + "@" + item['host'] + "/" + item['dbname'];
var mongoClient = new MongoClient.connect(uri,function(err,db){
if(!err) {
db.collectionNames(function(error,collections){
if(!err){
callback(null,collections);
}else{
callback(err,null);
}
});
} else {
callback(error,null);
}
});
},function(err,results){
res.send(results);
});
}
当看到mongodb服务器的控制台时,我可以看到太多的开放连接,并且在达到更高的数字时,它会崩溃。
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44311 #167 (87 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44312 #168 (88 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44313 #169 (89 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44314 #170 (90 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44315 #171 (91 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44316 #172 (92 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44317 #173 (93 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44318 #174 (94 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44319 #175 (95 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44320 #176 (96 connections now open)
Mon Oct 21 16:34:20 [conn167] end connection 127.0.0.1:44311 (95 connections now open)
Mon Oct 21 16:34:20 [conn169] end connection 127.0.0.1:44313 (94 connections now open)
Mon Oct 21 16:34:20 [conn171] end connection 127.0.0.1:44315 (94 connections now open)
Mon Oct 21 16:34:20 [conn173] end connection 127.0.0.1:44317 (92 connections now open)
Mon Oct 21 16:34:20 [conn175] end connection 127.0.0.1:44319 (92 connections now open)
Mon Oct 21 16:34:20 [conn168] end connection 127.0.0.1:44312 (90 connections now open)
Mon Oct 21 16:34:20 [conn170] end connection 127.0.0.1:44314 (89 connections now open)
Mon Oct 21 16:34:20 [conn172] end connection 127.0.0.1:44316 (88 connections now open)
Mon Oct 21 16:34:20 [conn174] end connection 127.0.0.1:44318 (87 connections now open)
Mon Oct 21 16:34:20 [conn176] end connection 127.0.0.1:44320 (86 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44321 #177 (87 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44322 #178 (88 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44323 #179 (89 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44324 #180 (90 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44325 #181 (91 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44326 #182 (92 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44327 #183 (93 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44328 #184 (94 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44329 #185 (95 connections now open)
Mon Oct 21 16:34:20 [initandlisten] connection accepted from 127.0.0.1:44330 #186 (96 connections now open)
答案 0 :(得分:1)
您可以使用async.mapLimit
来限制并行请求/连接的数量。
但是当你完成它们时,你也没有关闭你的连接,这也可能(也)是你没有连接的原因:
var mongoClient = new MongoClient.connect(uri,function(err,db){
if (!err) {
db.collectionNames(function(error,collections){
// done, close connection
db.close();
// call back with results or error
if (!error){
callback(null, collections);
} else {
callback(error, null);
}
});
} else {
callback(err, null);
}
});
(上面的代码也应修复err
和error
变量的混合