Skip to content

Add optional request governor for concurrency and rate limiting (1.2.0) - #2

Merged
erelsop merged 1 commit into
mainfrom
feat/rate-governor
Jul 23, 2026
Merged

Add optional request governor for concurrency and rate limiting (1.2.0)#2
erelsop merged 1 commit into
mainfrom
feat/rate-governor

Conversation

@erelsop

@erelsop erelsop commented Jul 22, 2026

Copy link
Copy Markdown
Owner

Summary

Adds an optional client-side request governor (1.2.0, purely additive) so consumers can stay within an upstream API's concurrency and rate limits during large or bursty workloads — e.g. the long paginated pulls this was built to support. No behavioural change when the new options are omitted.

Builds on #1 (merged).

New FetchEnhConfig options

All optional; all also accepted by setConfig(...):

  • concurrency — caps logical requests in flight via a fair FIFO semaphore. Applied once per logical request, not per retry attempt, so a request does not hold a slot while it sleeps between retries.
  • maxRps — caps requests started per second (minimum spacing of 1000 / maxRps ms between starts).
  • minIntervalMs — explicit minimum spacing between starts; takes precedence over maxRps.

Both gates honour a request's AbortSignal — a cancelled request waiting in the concurrency queue rejects promptly with an AbortError and never runs. With no option set, the governor is a zero-overhead pass-through.

Implementation

  • New src/core/governor.ts (RequestGovernor): FIFO semaphore + virtual "next allowed start" clock for rate spacing, no background timers.
  • Wired into _request around the logical-request promise, outside the retry loop, preserving the existing pre-await dedup-tracking guarantee (deduplicated callers share the single governed request).

Tests

New tests/fetchEnh.governor.test.ts — 9 tests: concurrency ceiling never exceeded, FIFO ordering, slot released on rejection, abort-while-queued, maxRps/minIntervalMs spacing, no-op fast path, and two FetchEnh integration tests (constructor + setConfig). Full suite: 261 passing across 11 suites.

Notes

  • Version bumped to 1.2.0; CHANGELOG.md updated. Not yet published to npm.

Introduces a client-side governor so consumers can stay within an upstream
API's concurrency and rate limits during large or bursty workloads such as
long paginated pulls.

New FetchEnhConfig options (all optional, also accepted by setConfig):
- concurrency: cap logical requests in flight via a fair FIFO semaphore,
  applied once per logical request so a slot is not held during retry backoff.
- maxRps: cap requests started per second (minimum spacing between starts).
- minIntervalMs: explicit minimum spacing; takes precedence over maxRps.

Both gates honour a request's AbortSignal (a cancelled queued request rejects
promptly and never runs), and the governor is a zero-overhead pass-through
when no option is set. Adds a governor test suite; full suite 261 passing.
@erelsop
erelsop merged commit f542552 into main Jul 23, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants