Defined in: packages/rate-limit/src/keyed-table.ts:117
A bounded table of rate-limit meters, one per client key.
The table owns three things a caller would otherwise write by hand. It clamps its clock once and builds every meter with that one reading, so a key it forgot and rebuilt sees the time the one it replaced would have seen. It tracks the instant each key’s meter becomes fresh, and forgets a key only at that instant, so forgetting never changes a later decision: a client that comes back after being dropped gets exactly the answer the meter the table dropped would have given. And when a new key arrives with no room and nothing forgettable, it applies the caller’s table-full choice instead of guessing.
Any object meeting RateMeter works as the meter, so two limits per client — requests per minute plus tokens per minute — are one meter that peeks both and takes both only when both allow. The table reads a meter’s freshness report and trusts it: it cannot verify the report without running the caller’s algorithm again. A custom meter that reports fresh too early makes the table forget a key that was still in debt, which forgives its clients; the documentation of such a meter owes that sentence.
take and peek of a held key run in O(1) expected time plus the meter’s
own. A take of a new key on a full table walks the held keys whose queued
fresh instant has passed, O(log n) each, where n is the number of keys held.
Example
Section titled “Example”const time = scriptedClock()const table = keyedRateLimitTable<string>({ maxKeyCount: 1_000, whenFull: 'refuse', clock: time.clock, meter: (clock) => fixedWindowRateLimiter({ capacity: 10, windowDuration: 1_000, clock }),})
table.take('client-a', 1) // { outcome: 'allowed', untracked: false }Type Parameters
Section titled “Type Parameters”K
Properties
Section titled “Properties”maxKeyCount
Section titled “maxKeyCount”
readonlymaxKeyCount:number
Defined in: packages/rate-limit/src/keyed-table.ts:119
The most keys the table holds meters for.
readonlysize:number
Defined in: packages/rate-limit/src/keyed-table.ts:121
Number of keys held, each with its own meter. O(1).
readonlytake: (key,weight) =>KeyedTableDecision
Defined in: packages/rate-limit/src/keyed-table.ts:138
Spend weight against the key’s meter, creating that meter when the table
has room or can forget a fresh key.
A weight that can never fit is answered before the table considers its own key bound, so a table too full to track a key still tells the caller the request is impossible rather than admitting it untracked.
Parameters
Section titled “Parameters”K
weight
Section titled “weight”number
Returns
Section titled “Returns”The meter’s decision with untracked: false, a refused
decision in refuse mode, or an allowed decision with untracked: true in
allow-untracked mode.
Throws
Section titled “Throws”With code INVALID_WEIGHT when
weight is not a positive safe integer. With code INVALID_CLOCK_READING
when the injected clock returns a non-finite number. The table records
nothing for the request after either.
readonlypeek: (key,weight) =>KeyedTableDecision
Defined in: packages/rate-limit/src/keyed-table.ts:151
Return the decision take would return for the key, adding no key and
spending nothing, and never report refused or untracked: a peek records
nothing for any key, so the table’s own key bound does not enter it.
A key the table does not hold answers with what its own fresh meter would
decide, which is the decision take would report if the table had room.
Parameters
Section titled “Parameters”K
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.