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.
Properties
Section titled “Properties”
readonlykind:"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.
capacity
Section titled “capacity”
readonlycapacity: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.
readonlytake: (weight) =>RateLimitDecision
Defined in: packages/rate-limit/src/rate-limit.ts:95
Spend weight when the request is allowed, otherwise report why not.
Parameters
Section titled “Parameters”weight
Section titled “weight”number
Returns
Section titled “Returns”Throws
Section titled “Throws”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.
readonlypeek: (weight) =>RateLimitDecision
Defined in: packages/rate-limit/src/rate-limit.ts:104
Return the decision take would return for weight without
spending it.
Parameters
Section titled “Parameters”weight
Section titled “weight”number
Returns
Section titled “Returns”Throws
Section titled “Throws”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.
freshAt
Section titled “freshAt”
readonlyfreshAt: () =>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.
Returns
Section titled “Returns”number
Throws
Section titled “Throws”With code INVALID_CLOCK_READING
when the injected clock returns a non-finite number.