Odysseus Limiter
Throttle limiting requests middleware for express apps. Can be backed by redis for maintainable and scalable apps.
Installation
$ npm install odysseus-limit
Examples
In the example below, after exceed 100 requests in 10 seconds, it will not proceed to next(). Each requests have time to live of 10 seconds. So, after 10 seconds from the first request, the limiter will proceed to next():
let OdysseusLimiter = ; app
You can block requests by key. Meaning that if requests with same key where exceed the rate specified, all next requests form the same key will be blocked (until ttl of some request will expire). For example:
let OdysseusLimiter = ;let options = { // req = requet express object return reqbodyusername; } amount: 100 ttl: 10000;app
And here you can block by IP:
let OdysseusLimiter = ;let options = { return reqconnectionremoteAddress; } amount: 100 ttl: 10000;app
Manage requets in redis for higher scalability:
let OdysseusLimiter = ;let options = store: host: 'localhost' port: 6379 ; amount: 100 ttl: 10000;app
Classes
LocalStore
- The requests save in memory.RedisStore
- implementation ofStore
. The requests are save in redis.- options:
host
- host of redisport
- port of redisclient
- predefined redis client
- options:
limit
- An express middleware that limits requests.
limit middleware options:
Property | Default | Description |
---|---|---|
amount | no default | as noted above |
ttl | no default | as noted above |
store | LocalStore |
where the requests are managed |
key | 'general' | give each request a key and throttle by that key |
More Complex Example
let OdysseusLimiter = ;let store = host: 'localhost' port: 6379;let options = store: store { return reqbodyusername; } { return reqbodyisAdmin ? 1000 : 50; } { return reqbodyisAdmin ? 10 : 10000; };app;app;
Test
The tests are written with mocha, supertest and chai under the folder 'test'
Run tests with:
npm test
Features
- Robust limiting.
- Highly scalable.
- Support limiting multiple routes and services.
- Open for customisation (store, key, amount and ttl).