We have been trying to debug an issue with our PQueue in our application for a few weeks now. We keep getting throttled by our API provider for going over their 2000/minute request limit. Even with setting our intervalCap
to 1900/minute, we were still breaching the limit regularly.
I modified one of the current tests to prove that somethings starts to go wrong when you increase the intervalCap
to numbers above 1. The current tests files use a cap of one which always passes. When you increase the intervalCap
to above ~100, the test starts failing randomly. Sometimes it passes, sometimes it fails. The higher the intervalCap
, the more likely it is to fail.
test('.add() - throttled, carryoverConcurrencyCount false large', async t => {
const result: number[] = [];
const testIntervals = 5;
const intervalCap = 10000;
const interval = 1000;
const queue = new PQueue({
intervalCap,
carryoverConcurrencyCount: false,
interval,
autoStart: false
});
const values = [...new Array(testIntervals * intervalCap).keys()];
values.forEach(async value => queue.add(async () => {
await delay(500);
result.push(value);
}));
queue.start();
t.is(queue.pending, intervalCap);
t.is(queue.size, (testIntervals - 1) * intervalCap);
t.deepEqual(result, []);
(async () => {
await delay(1.1 * interval);
t.is(queue.pending, intervalCap);
t.is(queue.size, (testIntervals - 2) * Number(intervalCap));
t.deepEqual(result, [...new Array(1 * Number(intervalCap)).keys()]);
})();
(async () => {
await delay(2.1 * interval);
t.is(queue.pending, intervalCap);
t.is(queue.size, (testIntervals - 3) * intervalCap);
t.deepEqual(result, [...new Array(2 * Number(intervalCap)).keys()]);
})();
(async () => {
await delay(3.1 * interval);
t.is(queue.pending, intervalCap);
t.is(queue.size, (testIntervals - 4) * intervalCap);
t.deepEqual(result, [...new Array(3 * Number(intervalCap)).keys()]);
})();
(async () => {
await delay(4.1 * interval);
t.is(queue.pending, intervalCap);
t.is(queue.size, (testIntervals - 5) * intervalCap);
t.deepEqual(result, [...new Array(4 * Number(intervalCap)).keys()]);
})();
await delay(5.1 * interval);
t.deepEqual(result, values);
});
It's worth noting the same behaviour is demonstrated if you use small intervals
(<100).
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too