![](/img/trans.png)
[英]javascript - how many emails we can send in nodeJs by using NodeMailer at a time?
[英]How many request we can make at a time using “request” middle ware in nodeJS application
我每5分钟运行一次cron作业,以从第3方API获取数据,它可以一次从NodeJS应用程序获得N个请求。 以下是详细信息和代码示例:
1>每5分钟运行一次cron作业:
const cron = require('node-cron');
const request = require('request');
const otherServices= require('./services/otherServices');
cron.schedule("0 */5 * * * *", function () {
initiateScheduler();
});
2>获取要为其发起请求的元素的列表。 可以接收N个元素。 我在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>开始启动对singleElement的请求。 请注意,如果我收到请求的成功(200)响应,则不需要任何回调。 我只需要成功更新我的数据库条目即可。
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
}
});
}
我已经检查了这个:
request-promise :但是,成功请求后,我不需要任何回调。 因此,我没有发现在代码中实现此优势。 让我知道您是否有任何积极的看法要补充。
在以下方面,我需要您的帮助:
在步骤2的arrayList中收到10个元素时,我已经检查了性能。现在,问题是我对在步骤2中开始接收100和1000个元素时所发生的情况没有任何清晰的认识。帮助您确定是否需要针对该场景更新代码,或者是否遗漏了任何会降低性能的内容。 另外,一次可以提出的最大请求数是多少。 您的任何帮助都是可观的。
谢谢!
AFAIK对数量的要求没有硬性限制。 但是,至少要考虑两件事:您的硬件限制(内存/ CPU)和远程服务器延迟(它是否能够在下一批之前5分钟内响应所有请求)。 如果不了解上下文,也无法预测您可能需要哪种缩放机制。
问题实际上更多是关于应用程序体系结构,而不是某些特定的代码,因此您可能想尝试软件工程而不是SO。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.