rate-limiter-flexible#RateLimiterMemory TypeScript Examples
The following examples show how to use
rate-limiter-flexible#RateLimiterMemory.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: limiter.ts From ts-di-starter with MIT License | 6 votes |
/**
* Create instance
*
* @param {CreateLimiterInterface} params
* @returns {Limiter}
*/
static create(params: CreateLimiterInterface): Limiter {
const rateLimiterMemory = new RateLimiterMemory({
keyPrefix: params.name,
points: Math.round(params.points / CONSTANTS.WORKER_COUNT),
duration: params.durationSec,
blockDuration: params.blockDurationSec,
execEvenly: params.execEvenly,
execEvenlyMinDelayMs: params.execEvenlyMinDelayMs,
});
return new Limiter({
name: params.name,
reason: params.reason,
getKey: params.getKey,
shouldConsume: params.shouldConsume,
shouldReset: params.shouldReset,
max: params.points,
blockDurationSec: params.blockDurationSec,
limiter: new RateLimiterRedis({
storeClient: params.redis,
keyPrefix: params.name,
points: params.points,
duration: params.durationSec,
blockDuration: params.blockDurationSec,
execEvenly: params.execEvenly,
execEvenlyMinDelayMs: params.execEvenlyMinDelayMs,
insuranceLimiter: rateLimiterMemory,
}),
});
}
Example #2
Source File: proxy.ts From NanoRPCProxy with GNU General Public License v3.0 | 5 votes |
limiter2 = new RateLimiterMemory({
keyPrefix: 'limit2',
points: settings.ddos_protection.request_limit, // limit each IP to x requests per duration
duration: Math.round(settings.ddos_protection.time_window/1000), // rolling time window in sec
})
Example #3
Source File: rate-limiter-ipc.ts From notabug with MIT License | 5 votes |
// tslint:disable-next-line: variable-name
_rateLimiters: Record<string, RateLimiterMemory>
Example #4
Source File: rateLimiter.ts From graphene with MIT License | 5 votes |
rlm = new RateLimiterMemory({
points: process.env.NODE_ENV === "test" ? 100 : 10,
duration: 60,
blockDuration: 30
})
Example #5
Source File: rate-limiter-ipc.ts From notabug with MIT License | 4 votes |
constructor() {
if (masterInstance) {
return masterInstance
}
this.ipcServer = new IPCServer(channel)
this._rateLimiters = {}
this.ipcServer.on('init', ([keyPrefix, dataStr], ack) => {
const existing = this._rateLimiters[keyPrefix]
const opts = JSON.parse(dataStr)
if (existing) {
if (opts.points) {
// @ts-ignore
existing.points = opts.points
}
if (opts.duration) {
// @ts-ignore
existing.duration = opts.duration
}
} else {
this._rateLimiters[keyPrefix] = new RateLimiterMemory(opts)
}
ack([])
})
this.ipcServer.on('consume', async ([keyPrefix, dataStr], ack) => {
const data = JSON.parse(dataStr)
try {
const res = await this._rateLimiters[keyPrefix].consume(
data.key,
data.pointsToConsume,
data.opts
)
ack({
channel,
keyPrefix,
data: res,
type: 'resolve'
})
} catch (err) {
ack({
channel,
keyPrefix,
data: err,
type: 'reject'
})
}
})
this.ipcServer.on('penalty', async ([keyPrefix, dataStr], ack) => {
const data = JSON.parse(dataStr)
try {
const res = await this._rateLimiters[keyPrefix].penalty(
data.key,
data.points,
data.opts
)
ack({
channel,
keyPrefix,
data: res,
type: 'resolve'
})
} catch (err) {
ack({
channel,
keyPrefix,
data: err,
type: 'reject'
})
}
})
this.ipcServer.on('reward', async ([keyPrefix, dataStr], ack) => {
const data = JSON.parse(dataStr)
try {
const res = await this._rateLimiters[keyPrefix].reward(
data.key,
data.points,
data.opts
)
ack({
channel,
keyPrefix,
data: res,
type: 'resolve'
})
} catch (err) {
ack({
channel,
keyPrefix,
data: err,
type: 'reject'
})
}
})
this.ipcServer.on('block', async ([keyPrefix, dataStr], ack) => {
const data = JSON.parse(dataStr)
try {
const res = await this._rateLimiters[keyPrefix].block(
data.key,
data.secDuration,
data.opts
)
ack({
channel,
keyPrefix,
data: res,
type: 'resolve'
})
} catch (err) {
ack({
channel,
keyPrefix,
data: err,
type: 'reject'
})
}
})
this.ipcServer.on('get', async ([keyPrefix, dataStr], ack) => {
const data = JSON.parse(dataStr)
try {
const res = await this._rateLimiters[keyPrefix].get(data.key, data.opts)
ack({
channel,
keyPrefix,
data: res,
type: 'resolve'
})
} catch (err) {
ack({
channel,
keyPrefix,
data: err,
type: 'reject'
})
}
})
this.ipcServer.on('delete', async ([keyPrefix, dataStr], ack) => {
const data = JSON.parse(dataStr)
try {
const res = await this._rateLimiters[keyPrefix].delete(
data.key,
data.opts
)
ack({
channel,
keyPrefix,
data: res,
type: 'resolve'
})
} catch (err) {
ack({
channel,
keyPrefix,
data: err,
type: 'reject'
})
}
})
masterInstance = this
}
Example #6
Source File: rate-limiter-ipc.ts From notabug with MIT License | 4 votes |
export class RateLimiterIPCWorker extends RateLimiterMemory {
keyPrefix: any
// tslint:disable-next-line: variable-name
_initiated: boolean
ipcClient: IPCClient
constructor(opts: IRateLimiterOptions) {
super(opts)
this.ipcClient = new IPCClient(channel)
process.setMaxListeners(0)
this._initiated = false
const rlOpts = getOpts.call(this)
this.ipcClient.send(
'init',
[rlOpts.keyPrefix, JSON.stringify(rlOpts)],
() => {
this._initiated = true
}
)
}
consume(key, pointsToConsume = 1, options = {}) {
return new Promise<RateLimiterRes>((resolve, reject) => {
this.ipcClient.send(
'consume',
[
this.keyPrefix,
JSON.stringify({
key,
pointsToConsume,
options
})
],
res => workerProcessResponse.call(this, res, resolve, reject)
)
})
}
penalty(key, points = 1, options = {}) {
return new Promise<RateLimiterRes>((resolve, reject) => {
this.ipcClient.send(
'penalty',
[
this.keyPrefix,
JSON.stringify({
key,
points,
options
})
],
res => workerProcessResponse.call(this, res, resolve, reject)
)
})
}
reward(key, points = 1, options = {}) {
return new Promise<RateLimiterRes>((resolve, reject) => {
this.ipcClient.send(
'reward',
[
this.keyPrefix,
JSON.stringify({
key,
points,
options
})
],
res => workerProcessResponse.call(this, res, resolve, reject)
)
})
}
block(key, secDuration, options = {}) {
return new Promise<RateLimiterRes>((resolve, reject) => {
this.ipcClient.send(
'block',
[
this.keyPrefix,
JSON.stringify({
key,
secDuration,
options
})
],
res => workerProcessResponse.call(this, res, resolve, reject)
)
})
}
get(key, options = {}) {
return new Promise<RateLimiterRes>((resolve, reject) => {
this.ipcClient.send(
'get',
[
this.keyPrefix,
JSON.stringify({
key,
options
})
],
res => workerProcessResponse.call(this, res, resolve, reject)
)
})
}
delete(key, options = {}) {
return new Promise<boolean>((resolve, reject) => {
this.ipcClient.send(
'delete',
[
this.keyPrefix,
JSON.stringify({
key,
options
})
],
res => workerProcessResponse.call(this, res, resolve, reject)
)
})
}
}