Node.js,Mongodb& MongoClient本地 - 有多少连接可能? "关闭套接字错误"

时间:2018-05-12 21:35:27

标签: javascript node.js mongodb database-connection

我在本地运行node.js和MongoDB。 CPU是AMD Ryzen 7 1800,内存为32.0 GB。 我的应用程序接受参数,然后调用函数以操作膳食计划。 在我扩大规模之前,一切都很完美和快速。

使用for循环可以在每个杂货店组合中启动初始函数,当我向MongoDB开放大约680个并行连接时,我开始出错。 命令行中的错误消息:"关闭套接字"

时出错

这有时只会发生。当我缩小到大约520(减少杂货店组合的数量)时,一切都非常快速和精细(输出大约100k食谱)。

我的问题: 1.为什么会这样? 2.我可以以某种方式配置Node.js / MongoClient / MongoDB,以便它利用我的装备的潜力吗?

这是一种示例代码提取:

//*File_Recipes* (Here I read out all recipes of one collection)

  //Import of the function that adapts the recipes
  var adapt = require("./File_AdaptIngredientOne")
  //Export of this module, as it will be called with different shop1&shop2- 
  //combos
  module.exports = async function (shop1, shop2) {  
   client.connect(url, function (err, client) {
   if(err) throw err;

   var db = client.db("database");
   var collection = db.collection("Recipes");
   var collection2 = db.collection("adaptedRecipes");

   var cursor = collection.find(query);
   //Now, I call the function adapt() with every found recipe
   //The function adapt needs to read data in MongoDB and insert&update 
   //instances
   cursor.ForEach(
      function(doc) {
          adaptIngredientOne(doc, collection, collection2, shop1, shop2);
          adaptIngredientTwo(doc, collection, collection2, shop1, shop2);
      });
   });
  };

//*File_AdaptIngredientOne* (Here, every ingredient of a recipe will be 
//adapted to a specific shop and then inserted into a collection

  module.exports = async function(doc, collection, collection2, shop1, 
  shop2){
  //Here I find a specific ingredient of a recipe in a specific shop
  var ingredientOne = await collection.findOne({
                         searchTerm: doc.Name + shop1
                         })
  // Now, the amount of this ingredient is updated
  var amount = doc.Amount * ingredientOne.Amount
  // After that, the recipe with an updated ingredient is saved as a new 
  //grocery store specific recipe
  await collection2.insertOne({
   Name: doc.Name,
   Amount: amount,
   ...
   })

//*File_Shops* (Here, the function from *File_Recipes* is called with 
//different grocery store combinations

  var recipes = require("./File_Recipes")
  var grocery_stores = ["Aldi", "Lidl", "Edeka", ....]

  //I now call the recipes function with all grocery store combinations

  for (i=0; i < grocery_stores.length; i++) {
   for(n=i; n < grocery_stores.length; n++) {
    shop1 = grocery_stores[i];
    shop2 = grocery_stores[n];
    recipes(shop1, shop2);
   }
  }

0 个答案:

没有答案