-
-
Notifications
You must be signed in to change notification settings - Fork 160
Consume points evenly
Roman edited this page May 8, 2021
·
2 revisions
This example is useful, when points should be consumed evenly over duration, e.g. your limiter has 10 points per hour, but it must allow to consume a point once per 6 minutes.
You can create two limiters:
- main limiter as points storage with allowance of 10 points per hour.
- interval limiter, which make sure only one point is consumed every 6 minutes.
Interval limiter rejects all tries, when point already consumed in current 6 minutes duration.
const {RateLimiterMemory} = require('rate-limiter-flexible');
module.exports = class RateLimiterEven {
constructor(options = {}, callback) {
const mainLimiter = new RateLimiterMemory(Object.assign({}, options, {
keyPrefix: 'main'
}), callback);
const intervalLimiter = new RateLimiterMemory(Object.assign({}, options, {
keyPrefix: 'interval',
points: 1,
duration: Math.ceil(this.duration / this.points)
}));
this._mainLimiter = mainLimiter;
this._intervalLimiter = intervalLimiter;
}
async consume(key, points = 1, options = {}) {
await this._intervalLimiter.consume(key, points, options);
return this._mainLimiter.consume(key, points, options);
}
penalty(key, points = 1) {
return this._mainLimiter.penalty(key, points);
}
reward(key, points = 1) {
return this._mainLimiter.reward(key, points);
}
get(key) {
return this._mainLimiter.get(key);
}
set(key, points, secDuration) {
return this._mainLimiter.set(key, points, secDuration);
}
block(key, secDuration) {
return this._mainLimiter.block(key, secDuration);
}
delete(key) {
return this._mainLimiter.delete(key);
}
}
You can also extend it from RateLimiterAbstract
, if you want to use it with RateLimiterQueue or RateLimiterUnion.
Get started
Middlewares and plugins
Migration from other packages
Limiters:
- Redis
- Memory
- DynamoDB
- Prisma
- MongoDB (with sharding support)
- PostgreSQL
- MySQL
- BurstyRateLimiter
- Cluster
- PM2 Cluster
- Memcached
- RateLimiterUnion
- RateLimiterQueue
Wrappers:
- RLWrapperBlackAndWhite Black and White lists
Knowledge base:
- Block Strategy in memory
- Insurance Strategy
- Comparative benchmarks
- Smooth out traffic peaks
-
Usage example
- Minimal protection against password brute-force
- Login endpoint protection
- Websocket connection prevent flooding
- Dynamic block duration
- Different limits for authorized users
- Different limits for different parts of application
- Block Strategy in memory
- Insurance Strategy
- Third-party API, crawler, bot rate limiting