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);
});
$ npm install redis-ratelimit
- Provides a distributed rate limit per key
- Redis sorted sets used for high performance
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
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.
The author is Chris Kinsman