Skip to content

RequestScheduler

Defined in: packages/rest/src/scheduler.ts:20

Serialises work per rate-limit bucket.

One request per bucket key is in flight at a time, so each request sees the limit state its predecessor’s response wrote. The queue slot is released on every exit path — including an abort raised while still waiting — because a slot that is never released wedges its bucket for the process lifetime. Buckets queue independently: a slow bucket never holds up another.

new RequestScheduler(options?): RequestScheduler

Defined in: packages/rest/src/scheduler.ts:30

Creates an empty scheduler.

SchedulerOptions = {}

Queue limits.

RequestScheduler

readonly static MAX_REMAPS: 2 = 2

Defined in: packages/rest/src/scheduler.ts:22

Maximum times a request may be moved to a different bucket queue.

get queued(): number

Defined in: packages/rest/src/scheduler.ts:44

Number of requests waiting for a slot.

number


get size(): number

Defined in: packages/rest/src/scheduler.ts:39

Number of bucket keys currently holding a queue slot.

number

run<T>(key, signal, path, task): Promise<T>

Defined in: packages/rest/src/scheduler.ts:97

Runs task once every earlier task for key has settled, or has freed its slot early through the release callback it was given.

T

string

Bucket key to serialise on.

AbortSignal | undefined

Cancellation signal honoured while queued.

string

Request path, used for cancellation error context.

(release) => Promise<T>

Work to run while holding the slot, given an idempotent callback that frees the slot before the task settles.

Promise<T>

Of kind "overloaded" when a queue limit is reached.


runResolved<T>(resolveKey, signal, path, task): Promise<T>

Defined in: packages/rest/src/scheduler.ts:64

Runs task under the bucket key that resolveKey reports at the moment the slot is acquired, not at enqueue time.

Discord can map several routes onto one bucket hash, and a route only learns its hash from a response. A request that queued under its route-derived key while another request discovered the shared hash would otherwise run concurrently with that bucket’s other traffic. Re-resolving after the wait moves it onto the right queue instead.

T

() => Promise<string>

Returns the current bucket key for this request.

AbortSignal | undefined

Cancellation signal honoured while queued.

string

Request path, used for cancellation error context.

(key, release) => Promise<T>

Work to run while holding the slot, given the final key and a callback that frees the slot early.

Promise<T>