Skip to content

Defined in: packages/rate-limit/src/rate-limit.ts:79

A rate-limit algorithm’s state for one key.

The same contract serves the two built-in-and-custom cases: a factory here builds one, and any object a caller supplies that meets these members works in a keyed table.

take and peek answer the same question and differ only in whether the weight is spent. Neither changes state on a denial.

readonly kind: "custom" | "fixed-window" | "sliding-window-log" | "sliding-window-counter" | "token-bucket"

Defined in: packages/rate-limit/src/rate-limit.ts:81

The algorithm this meter runs: a built-in kind, or 'custom' for a caller-supplied one.


readonly capacity: number

Defined in: packages/rate-limit/src/rate-limit.ts:86

The most weight one instant can admit from a fresh state. It bounds a single request: a weight above it is never-fits, in every kind.


readonly take: (weight) => RateLimitDecision

Defined in: packages/rate-limit/src/rate-limit.ts:95

Spend weight when the request is allowed, otherwise report why not.

number

RateLimitDecision

With code INVALID_WEIGHT when weight is not a positive safe integer, or INVALID_CLOCK_READING when the injected clock returns a non-finite number. The meter is unchanged after either.


readonly peek: (weight) => RateLimitDecision

Defined in: packages/rate-limit/src/rate-limit.ts:104

Return the decision take would return for weight without spending it.

number

RateLimitDecision

With code INVALID_WEIGHT when weight is not a positive safe integer, or INVALID_CLOCK_READING when the injected clock returns a non-finite number.


readonly freshAt: () => number

Defined in: packages/rate-limit/src/rate-limit.ts:117

The instant, in whole milliseconds of the injected clock, at which this meter’s state equals a fresh one’s, so dropping it cannot change a later decision.

It never decreases: a later call returns an instant at or after the one an earlier call returned. A keyed table relies on that, and keeps the instant it read when it created a key as a lower bound on every later one.

number

With code INVALID_CLOCK_READING when the injected clock returns a non-finite number.