0

私は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)
4

1 に答える 1

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

(上記のコードは、変数errerror変数の混合も修正する必要があります)

于 2013-10-21T12:49:05.853 に答える