Reputation: 320
I have something like this in Node.js and I'm using the Limiter to limit how many times this function can be executed in a minute. (10 times per min for example)
this.queue = [];
this.limiter = new RateLimiter(10, 60000);
someFunction() {
for(var element of this.queue) {
this.limiter.removeTokens(1, (err, remainingRequests) => {
//do something with element in queue and remove it from queue
});
}
}
Now I want this function to run immediately after something added to the queue, if it hits the limit, it should wait until the tokens reset and then continue executing for the remaining items in the queue. Keep in mind that items can be added to the queue all the time (assume 2 items added to the queue every sec all the time for example ). What is the best way to achieve these results? Is there a better implementation for this or any available packages that would be helpful?
Upvotes: 0
Views: 333
Reputation: 320
I was able to achieve the desired results using p-queue
this.queue = new PQueue({
concurrency: 1,
autoStart: true,
intervalCap: 10,
interval: 60000
});
this.queue.add(() => {
this.someFunctuon();
});
someFunction() will immediately start executing once it is added to the queue. The queue will execute 10 functions every minute. If more functions are added, it will wait 1 min before continuing.
Upvotes: 1
Reputation: 3856
Firstly, you should not remove elements from queue
inside the for loop where you are iterating over the elements of it.
I want this function to run immediately after something added to the queue
To achieve such kind of things, you should use something like BehaviorSubject
Upvotes: 0