The way the library operates is by always waiting the minimal time until the next request should be handled, regardless of the queue, but since we keep the cool down period as a number instead of future date, this can cause long wait periods that are unnecessary.
Lets follow the following scenario:
Set up a queue q1 that allows 4 requests per second with priority 2.
Set up a queue q2 that allows 10 requests per second with priority 1.
Add a request x1 to the q1 queue.
Add a request x2 to the q1 queue.
Add a request x3 to the q2 queue.
Add a request x4 to the q2 queue.
Expected behavior:
at t=0 x1 starts.
at t=0 x3 starts.
at t=100 x4 starts.
at t=250 x2 starts.
Current behavior:
at t=0 x1 starts.
at t=0 x3 starts.
at t=100 x4 starts.
at t=450 x2 starts.
I think the solution is to save on the queue the cool down future timestamp, and always wait until that date.
I will work on a PR later today to see how we can tackle this issue.
@energizer91 any thoughts?
The way the library operates is by always waiting the minimal time until the next request should be handled, regardless of the queue, but since we keep the cool down period as a number instead of future date, this can cause long wait periods that are unnecessary.
Lets follow the following scenario:
Expected behavior:
Current behavior:
I think the solution is to save on the queue the cool down future timestamp, and always wait until that date.
I will work on a PR later today to see how we can tackle this issue. @energizer91 any thoughts?
Reproducing test
```ts it('should properly schedule requests on multiple queues', async () => { const queue = new SmartQueue({ rules: { q1: { rate: 4, limit: 1, priority: 2 }, q2: { rate: 10, limit: 1, priority: 1 } } }); let r1Start = 0; let r2Start = 0; let r3Start = 0; let r4Start = 0; const request1 = () => { r1Start = Date.now(); return Promise.resolve(); }; const request2 = () => { r2Start = Date.now(); return Promise.resolve(); }; const request3 = () => { r3Start = Date.now(); return Promise.resolve(); }; const request4 = () => { r4Start = Date.now(); return Promise.resolve(); }; await Promise.all([ queue.request(request1, 'q1', 'q1'), queue.request(request2, 'q1', 'q1'), queue.request(request3, 'q2', 'q2'), queue.request(request4, 'q2', 'q2'), ]); expect(Math.abs(r3Start - r1Start)).is.lte(5); expect(Math.abs(r4Start - r1Start - 100)).is.lte(5); expect(Math.abs(r2Start - r1Start - 250)).is.lte(5); }); ```