RateLimiter

RateLimiter

An extensible rate limiter that can be used to prevent abuse of external services or limited resources. The storage methods are intended for override to support rate limiting across multiple sessions or devices if you have a shared storage backing.

Constructor

new RateLimiter(label, spec)

Source:
Parameters:
Name Type Description
label String The unique label used for singleton uses and storage keys.
spec RateLimiterSpec The configuration for this limiter.

Classes

RateLimiter

Methods

(async, static) singleton(label, spec) → {RateLimiter}

Source:
Create or return an existing instance.
Parameters:
Name Type Description
label String Unique identifier for this singleton.
spec RateLimiterSpec The spec to be used if, and only if, a new instance is created.
Returns:
A new or existing instance.
Type
RateLimiter

(async, abstract) getState() → {Object}

Source:
Subclasses can override to use permanent storage like IndexedDB. Otherwise state is just local to this instance.
Returns:
Meta data about the current usage.
Type
Object

(async) increment()

Source:
Increment usage by 1

(async) initialized()

Source:
Wait for init to complete.

(async) reset()

Source:
Reset the internal state. Use with caution

resumes() → {Number}

Source:
Returns:
Timestamp (ms) when a suspended limiter will wake.
Type
Number

(async, abstract) setState(state)

Source:
Subclasses can override to use permanent storage like IndexedDB. Otherwise state is just local to this instance.
Parameters:
Name Type Description
state Object Meta data about the current state.

suspended() → {boolean}

Source:
Returns:
Type
boolean

(async) wait()

Source:
Blocks until it is safe to run again. Note that this routine is concurrency-safe, so some calls for a given context may block longer than expected because of multiple accesses.

willSuspendFor() → {Number}

Source:
If the rate limiter will suspend on the next usage this will return the number of milliseconds of the expected wait. Note that the wait might actually end up being longer if increment is being called externally.
Returns:
Milliseconds of pending suspend or 0.
Type
Number