new RateLimiter(maxRate)
A utility for limiting the rate of repetitive send operations, such as bluetooth messages being sent to hardware devices. It uses the token bucket strategy: a counter accumulates tokens at a steady rate, and each send costs a token. If no tokens remain, it's not okay to send.
Parameters:
Name | Type | Description |
---|---|---|
maxRate |
number | the maximum number of sends allowed per second |
Members
_count :number
The current number of tokens in the bucket.
Type:
- number
_lastUpdateTime :number
The last time in milliseconds when the token count was updated.
Type:
- number
_maxTokens :number
The maximum number of tokens.
Type:
- number
_refillInterval :number
The interval in milliseconds for refilling one token. It is calculated so that the tokens will be filled to maximum in one second.
Type:
- number
Methods
okayToSend() → {boolean}
Check if it is okay to send a message, by updating the token count, taking a token and then checking if we are still under the rate limit.
Returns:
true if we are under the rate limit
- Type
- boolean