Skip to content

RateLimiter

RateLimiter = object

Defined in: Core/RateLimiter.ts:24

Paces fallible work against a sliding-window quota.

A call is admitted while fewer than limit calls have started in the last window. Beyond that, the default "wait" strategy queues the call and starts it when the oldest admitted call expires; "reject" settles immediately with RateLimiter.LimitError carrying retryAfterMs.

const github = RateLimiter.make({ limit: 60, window: Duration.minutes(1) });

const repo = await github.run(loadRepo("vercel/next.js"));

readonly available: () => number

Defined in: Core/RateLimiter.ts:47

Returns how many calls may start right now, between 0 and limit.

number


readonly reset: () => void

Defined in: Core/RateLimiter.ts:57

Forgets the recorded window and admits every queued caller in submission order.

Intended for tests and operational overrides: it deliberately allows more than limit calls to have started inside one window.

void

RateLimiter.available to inspect the window instead of clearing it.


readonly run: {<E, A>(task): Deferred<Result<E | LimitError, A>>; <E, A>(task, options): Deferred<Result<Aborted | LimitError | E, A>>; }

Defined in: Core/RateLimiter.ts:36

Starts a fallible task when the quota allows it.

The task is lazy, so it does not start before a permit exists. With the "wait" strategy the returned deferred settles once the task does; with "reject" it settles immediately with RateLimiter.LimitError.

<E, A>(task): Deferred<Result<E | LimitError, A>>

E

A

Result<E, A>

Deferred<Result<E | LimitError, A>>

<E, A>(task, options): Deferred<Result<Aborted | LimitError | E, A>>

E

A

Result<E, A>

AbortSignal

Deferred<Result<Aborted | LimitError | E, A>>

task

The fallible work to run under the quota.

options

Optional cancellation. Aborting a queued call removes it without using a permit.

RateLimiter.available to read the remaining permits.