Class: SlidingWindowCounter
Defined in: throttle.ts:776 Sliding-window counter for per-key rate limiting. Maintains acurrent and previous window count per key and
estimates the weighted request rate over the trailing
windowMs ms by interpolating between the two windows. This
provides smoother enforcement than a fixed-window counter (which
lets twice the limit through across a window boundary) without the
memory cost of a true sliding-window log.
Calls a periodic cleanup every windowMs ms to drop stale
entries — note that this means the limiter holds a Node timer
for its entire lifetime. Long-lived processes are fine; for
short-lived workers, manage instances explicitly or you’ll keep
the event loop alive.
Example
Constructors
Constructor
new SlidingWindowCounter(Defined in: throttle.ts:786windowMs,maxRequests):SlidingWindowCounter
Parameters
windowMs
number
Sliding-window length in milliseconds.
maxRequests
number
Maximum allowed weighted count per window
per key.
Returns
SlidingWindowCounter
Methods
check()
check(Defined in: throttle.ts:806 Atomically increment the counter forkey):object
key and decide whether
to allow the request based on the trailing weighted count.
Parameters
key
string
Returns
object
{ allowed, remaining, resetAt } where:
allowed—trueif under the limit;falseif rejected (counter is not incremented in this case).remaining— best-effort lower bound on how many more requests fit in the current window for this key.resetAt— Unix epoch ms when the current fixed window boundary rolls over.
allowed
allowed: boolean
remaining
remaining: number
resetAt
resetAt: number
getStats()
getStats(): object
Defined in: throttle.ts:877
Snapshot of currently-tracked keys.
Returns
object
{ activeKeys, keys }. The keys array is a one-shot
copy and not kept in sync with future mutations.
activeKeys
activeKeys: number
keys
keys: readonly string[]
reset()
reset(Defined in: throttle.ts:852 Forget all state forkey):void
key. The next check(key) starts fresh.
Useful for admin/test reset paths and for clearing limits when
a user upgrades to a higher tier.
Parameters
key
string
Returns
void