简体   繁体   English

使用nodeJS应用程序中的“请求”中间件一次可以发出多少个请求

[英]How many request we can make at a time using “request” middle ware in nodeJS application

I am running a cron job every 5 mins to get data from 3rd party API, It can be N number of request at a time from NodeJS application. 我每5分钟运行一次cron作业,以从第3方API获取数据,它可以一次从NodeJS应用程序获得N个请求。 Below are the details and code samples: 以下是详细信息和代码示例:

1> Running cron Job every 5 mins: 1>每5分钟运行一次cron作业:

const cron = require('node-cron');
const request = require('request');
const otherServices= require('./services/otherServices');
cron.schedule("0 */5 * * * *", function () {
  initiateScheduler();
});

2> Get the list of elements for which I want to initiate the request. 2>获取要为其发起请求的元素的列表。 Can receive N number of elements. 可以接收N个元素。 I have called request function ( getSingleElementUpdate() ) in the forEach loop 我在forEach循环中调用了请求函数( getSingleElementUpdate()

var initiateScheduler = function () {
  //Database call to get elements list
  otherServices.moduleName()
    .then((arrayList) => {
      arrayList.forEach(function (singleElement, index) {
        getSingleElementUpdate(singleElement, 1);
      }, this);
    })
    .catch((err) => {
      console.log(err);
    })
}

3> Start initiating the request for singleElement. 3>开始启动对singleElement的请求。 Please note I don't need any callback if I received a successful (200) response from the request. 请注意,如果我收到请求的成功(200)响应,则不需要任何回调。 I just have to update my database entries on success. 我只需要成功更新我的数据库条目即可。

var getSingleElementUpdate = function (singleElement, count) {
  var bodyReq = {
    "id": singleElement.elem_id
  }
  var options = {
    method: 'POST',
    url: 'http://example.url.com',
    body: bodyReq,
    dataType: 'json',
    json: true,
    crossDomain: true
  };
  request(options, function (error, response, body) {
    if (error) {
      if (count < 3) {
        count = count + 1;
        initiateScheduler(singleElement, count)
      }
    } else{
      //Request Success
      //In this: No callback required
      // Just need to update database entries on successful response
    }
  });
}

I have already checked this: 我已经检查了这个:

request-promise : But, I don't need any callback after a successful request. request-promise :但是,成功请求后,我不需要任何回调。 So, I didn't find any advantage of implementing this in my code. 因此,我没有发现在代码中实现此优势。 Let me know if you have any positive point to add this. 让我知道您是否有任何积极的看法要补充。

I need your help with the following things: 在以下方面,我需要您的帮助:

I have checked the performance when I received 10 elements in arrayList of step 2. Now, the problem is I don't have any clear vision about what will happen when I start receiving 100 and 1000 of elements in step 2. So, I need your help in determining whether I need to update my code for that scenario or not or is there anything I missed out which degrade the performance. 在步骤2的arrayList中收到10个元素时,我已经检查了性能。现在,问题是我对在步骤2中开始接收100和1000个元素时所发生的情况没有任何清晰的认识。帮助您确定是否需要针对该场景更新代码,或者是否遗漏了任何会降低性能的内容。 Also, How many maximum requests I can make at a time. 另外,一次可以提出的最大请求数是多少。 Any help from you is appreciable. 您的任何帮助都是可观的。

Thanks! 谢谢!

AFAIK there is no hard limit on a number of request. AFAIK对数量的要求没有硬性限制。 However, there are (at least) two things to consider: your hardware limits (memory/CPU) and remote server latency (is it able to respond to all requests in 5 mins before the next batch). 但是,至少要考虑两件事:您的硬件限制(内存/ CPU)和远程服务器延迟(它是否能够在下一批之前5分钟内响应所有请求)。 Without knowing the context it's also impossible to predict what scaling mechanism you might need. 如果不了解上下文,也无法预测您可能需要哪种缩放机制。

The question is actually more about app architecture and not about some specific piece of code, so you might want to try softwareengineering instead of SO. 问题实际上更多是关于应用程序体系结构,而不是某些特定的代码,因此您可能想尝试软件工程而不是SO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM