Node.JS: The Best Way to Handle Maximum Requests Per Minute Per Server
Sorry if this question has already been answered and I hope I am not breaking SO rules, if so, sorry in advance ... I was wondering what is the best way to handle the query delimiter? I've seen a bunch of throttles and speed limiters on the internet, but I'm not sure how to do this or applicable in my case.
I am making a bunch of [OUTGOING] request - array based promises and on the server I can only make 90 requests per minute. My request promises generated by this command: return Promise.all(array.map(request))
.
I thought about it:
var i = 0;
return rp({
url: uri,
json: true,
}).then((data) => {
if (i <=90) {
i ++;
return data;
} else {
return i;
}
});
but i'm not sure if this would be a really efficient way to handle this plus i'm not sure how to handle the time relation yet ...: S
Thanks in advance for your help and sorry that I'm still a huge newbie ...
source to share
If requests are started from different parts of the code, it can be helpful to implement sth as a server queue that waits for a request until it is resolved. General handler:
var fromUrl = new Map();
function Server(url, maxPerMinute){
if(fromUrl.has(url)) return fromUrl.get(url);
fromUrl.set(url,this);
this.tld = url;
this.maxPerMinute = maxPerMinute;
this.queue = [];
this.running = false;
}
Server.prototype ={
run(d){
if(this.running && !d) return;
var curr = this.queue.shift();
if(!curr){
this.running = false;
return;
}
var [url,resolve] = curr;
Promise.all([
request(this.tld + url),
new Promise(res => setTimeout(res, 1000*60/this.maxPerMinute)
]).then(([res]) => {
resolve(res);
this.run(true);
});
},
request(url){
return new Promise(res => {
this.queue.push([url,res]);
this.run();
});
}
};
module.exports = Server;
Used like this:
var google = new require("server")("http://google.com");
google.maxPerMinute = 90;
google.request("/api/v3/hidden/service").then(res => ...);
source to share
Possibly just 90 requests per minute. You can pseudo-recursive promise using the function:
function multiRequest(urls, maxPerMinute){
return new Promise(function(cb){
var result = [];
//iterate recursively
(function next(i){
//if finished resolve promise
if(i>=urls.length) return cb(result);
//get all requests
var requests = Promise.all(urls.slice(i,i+maxPerMinute).map(request));
//if the requests are done, add them to result
requests.then(data=>result.push(...data));
//if the requests + one minute done, conginue with next
Promise.all([
requests,
new Promise(res=>setTimeout(res,1000*60))
] ).then(_=>next(i+maxPerMinute))
})(0);
});
}
Use it like this:
multiRequests(["google.com","stackoverflow.com"],90)
.then(([google,so])=>...);
source to share
You can use setInterval
. Check out the documentation here .
var requestCount = 0;
setInterval(function(){
// Every 60 seconds, reset the count
requestCount = 0;
}, 60000);
// There needs to be an additional check before calling rp,
// that checks for requestCount > 90, and returns before starting the request.
rp({
url: uri,
json: true,
})
.then((data) => {
requestCount++;
return data;
});
source to share