https://github.com/aftership/rate-limiter
Redis rate limiter for Node.js
https://github.com/aftership/rate-limiter
Last synced: about 1 year ago
JSON representation
Redis rate limiter for Node.js
- Host: GitHub
- URL: https://github.com/aftership/rate-limiter
- Owner: AfterShip
- License: mit
- Created: 2015-03-17T09:22:30.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T03:35:06.000Z (over 3 years ago)
- Last Synced: 2025-04-11T16:37:10.761Z (about 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 1.58 MB
- Stars: 11
- Watchers: 22
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rate-limiter
[](https://travis-ci.org/AfterShip/rate-limiter)
[](https://app.fossa.com/projects/git%2Bgithub.com%2FAfterShip%2Frate-limiter?ref=badge_shield)
[]()
[]()
[]()
Rate limit for Node.js, with ioredis client
## Installation
```
npm install @aftership/rate-limiter
```
## Dependency
The client must be an instance of `ioredis` library.
## Examples
```javascript
const Limiter = require('@aftership/rate-limiter');
const Redis = require('ioredis');
const redisClient = new Redis({
port: 6379,
host: '127.0.0.1',
db: 0
});
redisClient.on('connect', (err) => {
if (err) {
console.log(err);
return;
}
// limit to 2 request per every 10s
const limiter = new Limiter({
redisClient,
key: 'the-user-api-key',
limit: 2, // default is 10
duration: 10 // default is 60s
});
limiter
.get()
.then((result) => {
console.log(result);
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.`);
}
process.exit(0);
})
.catch((e) => {
console.log(e);
});
});
```
## Change log
Please refer to release page
## License
Copyright (c) 2019 AfterShip
Licensed under the MIT license.
[](https://app.fossa.com/projects/git%2Bgithub.com%2FAfterShip%2Frate-limiter?ref=badge_large)