Rate limit for Node.js, with ioredis client
npm install @aftership/rate-limiter
The client must be an instance of ioredis
library.
var Limiter = require('@aftership/rate-limiter');
var redis = require('ioredis');
var redis_client = redis.createClient({
host: 'localhost',
port: 6379,
db: 0
});
redis_client.on('connect', function(err) {
if (err) {
console.log(err);
} else {
// limit to 2 request per every 10s
var limiter = new Limiter({
redis_client: redis_client,
key: 'the-user-api-key',
limit: 2, // default is 10
duration: 10 // default is 60s
});
limiter.get(function(err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
// { limit: 2, remaining: 1, reset: 10 }
if (result.remaining >= 0) {
console.log('I can do the request!');
} else {
console.log('I run out of limit! Try again after ' + result.reset + ' second.');
}
}
});
}
});
npm test
Please refer to CHANGELOG.md
Copyright (c) 2016 AfterShip
Licensed under the MIT license.