Rate limiting / throttling based using Redis
var ratelimit = require('redis-ratelimit')
var async = require('async')
var count = 0;
async.doWhilst(function(done) {
ratelimit.check('counter', 10, 2, function(err, limited) {
if(limited)
{
// Don't do anything, wait some amount of time
// and check rate limit again
setTimeout(done, 1000*5);
}
else
{
// Do work
count++;
done();
}
})
}, function() {
return count < 10;
}, function(err) {
// Done
process.exit(0);
});
Installation
$ npm install redis-ratelimit
Features
- Provides a distributed rate limit per key
- Redis sorted sets used for high performance
Documentation
configure(port, host, options)
Optionally configures the underlying redis instance used by the rate limiter.
Arguments
port
- Port redis is listening onhost
- Host redis is onoptions
- Options hash passed through to underlying redis createClient() call
check(key, windowInSeconds, limit, callback)
Checks if the specified key over or under the rate limit. If over rate limit the call to check is not counted against the rate limit
Arguments
key
- Unique key the rate limit is associated withwindowInSeconds
- The length of the window during which the calls are rate limited. This is not a bucket but a sliding windowlimit
- Number of calls that are allowed before the rate limit kicks incallback(err, limited)
- A callback which indicates if the rate limit has kicked in or not. If an error occurred limited will be set to true. If limited returns true the call to check() does not count against the rate limit.
People
The author is Chris Kinsman