A rolling rate limit using Redis. Original idea from Peter Hayes. Uses a lua script for atomic operations and to prevent blocked actions from substracting from the bucket.
Compatible with Redis Cluster.
var RollingLimit = require('redis-token-bucket-ratelimiter');
Creates a new RollingLimit instance.
Options:
limit
: (required) maximum of allowed uses ininterval
interval
: (required) millisecond duration for thelimit
redis
: (required) an instance of RedisClientprefix
: (optional) a string to prepend beforeid
for each keyforce
: (optional) a boolean to force an accept, but draining the bucket if necessary
Takes a token from the limit's bucket for id
in redis and returns a promise with
the limit response object:
numLeft
: (number >= 0) the number of tokens left in the bucketrejected
: (boolean) whether or not the request was rejected- `retryDelta
amount
: the number of tokens to take from the bucket and defaults to1
.
If you want to get the count of tokens left, send in an amount
of 0
.
Synchronously returns a fake-but-complete response object, with the supplied max for a limit