@dutu/rate-limiter is an implementation of a rate limiter that supports token bucket, rolling window and fixed window
The module adheres to Semantic Versioning.
See github releases, where all notable changes are documented.
npm install --save "git+https://github.com/dutu/rate-limiter.git"
or
npm install --save "@dutu/rate-limiter"
import { RollingWindowLimiter } from '@dutu/rate-limiter'
import { TokenBucketLimiter } from '@dutu/rate-limiter'
import { FixedWindowLimiter } from '@dutu/rate-limiter'
or
const RollingWindowLimiter = require('@dutu/rate-limiter').RollingWindowLimiter
const TokenBucketLimiter = require('@dutu/rate-limiter').TokenBucketLimiter
const FixedWindowLimiter = require('@dutu/rate-limiter').FixedWindowLimiter
const limiter = new RollingWindowLimiter({ tokensPerInterval, interval, stopped = false })
const limiter = new FixedWindowLimiter({ tokensPerInterval, interval, stopped = false })
The Reservoir Interval starts from the moment
getTokens()
is called for the first time.
const limiter = new TokenBucketLimiter({ bucketSize, tokensPerInterval, interval, stopped = false })
The rate limiter can be created with initial state 'stopped' and with no tokens, by specifying the parameter
stopped
set totrue
(default isfalse
). When stopped, the rate limiter needs to be restarted by calling the methodstart()
Boolean value indicating if the rate limiter is stopped.
Tries to remove a number af tokens and returns immediately a boolean value indicating if the token removal was successful.
Returns the number of available tokens
Returns the number of milliseconds until the time when count
tokens will be available. If the tokens are immediately available, it returns 0
(zero).
If the rate limiter is stopped and the tokens are not immediately available, the method returns undefined
.
Returns a promise which resolves when count
tokens become available. If the rate limiter is stopped, the promise resolves after the rate limiter is restarted and the tokens are available.
Reinitializes the rate limiter, the reservoir is set to the initial size. If the limiter has been previously stopped, it is restarted.
Stops the rate limiter. Rate limiter will add no more tokens. If parameter false
is specified, the existing tokens can still be used until exhausted.
(Re)starts the rate limiter. Adding new tokens is resumed.
import { RollingWindowLimiter } from '@dutu/rate-limiter'
async function testToken() {
const limiter = new RollingWindowLimiter({ tokensPerInterval: 20, interval: 1000 * 10 })
debug(limiter.getTokens())
debug(limiter.tryRemoveTokens(10))
debug(limiter.tryRemoveTokens(32))
debug(limiter.getTokens())
await new Promise((resolve) => setTimeout(() => resolve(), 2000))
debug(limiter.tryRemoveTokens(10))
debug(limiter.getTokens())
debug(await limiter.awaitTokens(8))
debug(await limiter.awaitTokens(20))
}
testToken()