-
-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Is your feature request related to a problem? Please describe.
Sometimes it is important to cap the number of operations executed within a period of time.
One use case is API operations. APIs often have rate limiting, which restricts how many API requests are allowed in a specific period of time. For example, an API may only allow 100 requests per minute. If 1000 requests need to be made, logic indicates it should take 10 minutes to run all the requests (100 per minute).
Describe the solution you'd like
I would like to configure throttling. The queue automatically batch tasks to fit within their time-restricted window or maximum
concurrent operations window. For example:
Rate Limiting
const tasks = new Queue()
tasks.ratelimit(100, 60 * 1000) // 100 requests, 60 seconds/60K milliseconds
for (i=0; i < 1000; i++) {
tasks.add(() => ... do something...)
}
let plan = tasks.plan()
console.log(`Expected to take ${plan.executionDuration} milliseconds`)
tasks.on('complete', () => console.log('Done'))
tasks.run()Sometimes limits aren't time sensitive, but only X number of operations can be performed at the same time (i.e. maximum simultaneous operations). This may look like:
Maximum Concurrency
const tasks = new Queue()
tasks.limit(10) // 10 operations at a time
...The code above would only allow 10 simultaneous operations to work at the same time.
Describe alternatives you've considered
There are no alternatives in NGN. It must all be done by hand or with another library.