refactor: remove unnecessary max/min function
This commit is contained in:
parent
4af9c90d12
commit
0ef7f14fa9
|
@ -49,40 +49,37 @@ export class RateLimiterService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Short-term limit
|
// Short-term limit
|
||||||
const min = async () => {
|
if (limitation.minInterval != null) {
|
||||||
if (limitation.minInterval != null) {
|
const info = await this.checkLimiter({
|
||||||
const info = await this.checkLimiter({
|
id: `${actor}:${limitation.key}:min`,
|
||||||
id: `${actor}:${limitation.key}:min`,
|
duration: limitation.minInterval * factor,
|
||||||
duration: limitation.minInterval * factor,
|
max: 1,
|
||||||
max: 1,
|
db: this.redisClient,
|
||||||
db: this.redisClient,
|
});
|
||||||
});
|
|
||||||
this.logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`);
|
this.logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`);
|
||||||
if (info.remaining === 0) {
|
|
||||||
// eslint-disable-next-line no-throw-literal
|
if (info.remaining === 0) {
|
||||||
throw { code: 'BRIEF_REQUEST_INTERVAL', info };
|
// eslint-disable-next-line no-throw-literal
|
||||||
}
|
throw { code: 'BRIEF_REQUEST_INTERVAL', info };
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
// Long term limit
|
// Long term limit
|
||||||
const max = async () => {
|
if (limitation.duration != null && limitation.max != null) {
|
||||||
if (limitation.duration != null && limitation.max != null) {
|
const info = await this.checkLimiter({
|
||||||
const info = await this.checkLimiter({
|
id: `${actor}:${limitation.key}`,
|
||||||
id: `${actor}:${limitation.key}`,
|
duration: limitation.duration * factor,
|
||||||
duration: limitation.duration * factor,
|
max: limitation.max / factor,
|
||||||
max: limitation.max / factor,
|
db: this.redisClient,
|
||||||
db: this.redisClient,
|
});
|
||||||
});
|
|
||||||
this.logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`);
|
|
||||||
if (info.remaining === 0) {
|
|
||||||
// eslint-disable-next-line no-throw-literal
|
|
||||||
throw { code: 'RATE_LIMIT_EXCEEDED', info };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
await min();
|
this.logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`);
|
||||||
await max();
|
|
||||||
|
if (info.remaining === 0) {
|
||||||
|
// eslint-disable-next-line no-throw-literal
|
||||||
|
throw { code: 'RATE_LIMIT_EXCEEDED', info };
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue