Token bucket rate limiter implementation.
The token bucket algorithm allows for bursting up to the capacity, while maintaining a steady-state rate equal to the refill rate.
const limiter = new RateLimiter({ capacity: 100, refillRate: 10, refillIntervalMs: 1000,});if (limiter.tryAcquire()) { // Proceed with operation} else { // Rate limited, reject or queue} Copy
const limiter = new RateLimiter({ capacity: 100, refillRate: 10, refillIntervalMs: 1000,});if (limiter.tryAcquire()) { // Proceed with operation} else { // Rate limited, reject or queue}
Attempts to acquire a token.
Number of tokens to acquire (default: 1)
True if tokens were acquired, false if rate limited
Gets the current state of the rate limiter.
The current rate limiter state
Resets the rate limiter to full capacity. Useful for testing or after configuration changes.
Token bucket rate limiter implementation.
The token bucket algorithm allows for bursting up to the capacity, while maintaining a steady-state rate equal to the refill rate.
Example