Allow changing intervalCap at runtime - #252
Conversation
Adds a get/set intervalCap accessor, mirroring the existing concurrency accessor, so the rate limit can be adjusted after the queue is created. Increasing the cap immediately lets already-queued tasks start instead of waiting for the next interval tick. Closes sindresorhus#177
|
The Node.js 20 CI run failed on `strict mode multiple complete intervals` in `test/strict.ts`: ``` That test asserts tight 100ms timing windows for strict-mode scheduling and doesn't touch anything this PR changes (it doesn't use the new `intervalCap` setter, and this diff doesn't modify strict-mode tick/timer logic at all — only the read-only-ness of `intervalCap` and the internal `#isIntervalIgnored` recomputation). I ran `test/strict.ts` locally 5x against this branch and it passed every time, so this looks like CI-runner timing jitter rather than something caused by this change. Happy to rebase/retrigger if a maintainer re-runs the job, or let me know if you'd like anything else checked. |
|
Thanks, but I'm not interested in reviewing fully AI generated PRs. |
Summary
Adds a
get/setintervalCapaccessor onPQueue, mirroring the existingconcurrencyaccessor, so the rate limit can be adjusted after the queue has been created.Closes #177, which asks for exactly this ("The only thing required for this would be to control
#intervalCapproperty ofPQueueat runtime").Changes
#intervalCapis no longerreadonly; addedget intervalCap()/set intervalCap()with the same validation the constructor already does (must be a number >= 1, and must stay finite whenstrictis enabled).#isIntervalIgnored(an internal flag derived fromintervalCap/interval) is now recomputed via a small#updateIsIntervalIgnored()helper wheneverintervalCapchanges, so it doesn't go stale.intervalCapcalls the existing#processQueue(), so raising the cap immediately lets already-queued tasks start rather than waiting for the next interval tick (same behavior as the existingconcurrencysetter).intervalCapcan be changed at runtime, and added the.intervalCapentry to the properties list (same pattern as.concurrency).Testing
enforce number in queue.intervalCap(mirrors the existingenforce number in queue.concurrencytest).queue.intervalCap requires a finite value when strict(mirrors the constructor's equivalent guard).queue.intervalCap can be changed at runtime, which proves the behavior end-to-end: withintervalCap: 1, only 1 of 4 queued tasks starts; raisingintervalCapto3immediately starts 2 more without waiting for an interval tick.source/index.tsmakes the three new tests fail (intervalCapis currently read-only), confirming they exercise the reported gap; with the fix applied,node --import=tsx/esm --test test/*.tspasses all 209 tests.tscandtsdboth pass clean with no errors.xocurrently fails on this checkout with "was not found by the project service" parsing errors onbench.tsand everything undertest//test-d/— verified this is pre-existing and unrelated to this diff (same errors occur on a clean checkout ofmainwith no changes applied), likely an environment/path quirk in this sandbox rather than a real lint issue.Notes
intervalitself is intentionally left read-only in this PR to keep the change small — it drives timer/window scheduling (#intervalId/#timeoutId) which needs more careful handling to change safely mid-window. Happy to follow up separately if that's wanted too.