README
Appolo RateLimit Module
RateLimit module for appolo built with Redis
Installation
npm i @appolo/rate-limiter
Options
| key | Description | Type | Default |
|---|---|---|---|
id |
RateLimiter injection id |
string |
rateLimiter |
connection |
redis connection string |
string |
|
keyPrefix |
redis key prefix | string |
rl |
maxBuckets |
max number of buckets | number |
600 |
minBucketInterval |
min bucket interval in milisec | number |
5000 |
in config/modules/all.ts
import {RateLimiterModule} from '@appolo/rate-limiter';
export = async function (app: App) {
await app.module(new RateLimiterModule({connection:process.env.REDIS}));
}
Usage
import {define, singleton,inject} from 'appolo'
import {RateLimiter} from "@appolo/rate-limiter";
@define()
@singleton()
export class SomeManager {
@inject() rateLimiter: RateLimiter;
async checkLimits(): Promise<boolean> {
let result = await this.rateLimiter.reserve({
key:"someKey",
roles:[{
interval:10 * 60 * 1000, //10 min
limit:100,
}]
})
return result.isValid;
}
}
RateLimiter
Reserve
reserve key and return results object of the key rate limit usage
Options
key- key stringroles- array of rolesinterval- number of miliseconds in a sliding windowlimit- max number of items in a sliding windowspread-trueto spread the limit evenly across interval buckets - defaultfalsereserve- the amount items to reserve default1bucket- bucket interval in milisec - if not defined will be set automaticallystart- if we in fixed windowx - start point of the interval defaultDate.now()
- type -
RateLimitType- sliding window or fixed window - default sliding window
Results
isValid- true if all the limits are validresults- array of limit resultscount- current limit countbucket- bucket interval in milisecremaining- remaining limit of the keyrateLimit- if spread defined the rate limit per bucketrate- if spread defined - current ratereset- the number of milisec until the limit will reset to its maximum capacityretry- the number of milisec until bucket reset
import {define, singleton,inject} from 'appolo'
import {RateLimiter} from "@appolo/rate-limiter";
@define()
@singleton()
export class SomeManager {
@inject() rateLimiter: RateLimiter;
async checkLimits(): Promise<boolean> {
let result = await this.rateLimiter.reserve({
key:"someKey",
limits:[{
interval:10 * 60 * 1000, //10 min
limit:100,
spread:true,
reserve:100
}]
})
return result.isValid;
}
}
Check
Checks current usage of the key the counters won't be updated
Options
same as Reserve options
Results
same as Reserve results
import {define, singleton,inject} from 'appolo'
import {RateLimiter} from "@appolo/rate-limiter";
@define()
@singleton()
export class SomeManager {
@inject() rateLimiter: RateLimiter;
async checkLimits(): Promise<boolean> {
let result = await this.rateLimiter.check({
key:"someKey",
limits:[{
interval:10 * 60 * 1000, //10 min
limit:100,
spread:true,
reserve:100
}]
})
return result.isValid;
}
}
Cancel
cancel rate limit and clean all for given key
await this.rateLimiter.clean("someKey")