Constructor
new RateLimiter(label, spec)
Parameters:
Name | Type | Description |
---|---|---|
label |
String | The unique label used for singleton uses and storage keys. |
spec |
RateLimiterSpec | The configuration for this limiter. |
Classes
Methods
(async, static) singleton(label, spec) → {RateLimiter}
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}
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()
Increment usage by 1
(async) initialized()
Wait for init to complete.
(async) reset()
Reset the internal state. Use with caution
resumes() → {Number}
Returns:
Timestamp (ms) when a suspended limiter will wake.
- Type
- Number
(async, abstract) setState(state)
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}
Returns:
- Type
- boolean
(async) wait()
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}
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