![](/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.