refactor: wrap ratelimiter with promise
This commit is contained in:
parent
28c8d318e1
commit
d8844aed6f
|
@ -30,6 +30,18 @@ export class RateLimiterService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@bindThis
|
||||||
|
private checkLimiter(options: Limiter.LimiterOption): Promise<Limiter.LimiterInfo> {
|
||||||
|
return new Promise<Limiter.LimiterInfo>((resolve, reject) => {
|
||||||
|
new Limiter(options).get((err, info) => {
|
||||||
|
if (err) {
|
||||||
|
return reject({ code: 'ERR', info });
|
||||||
|
}
|
||||||
|
resolve(info);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@bindThis
|
@bindThis
|
||||||
public limit(limitation: IEndpointMeta['limit'] & { key: NonNullable<string> }, actor: string, factor = 1) {
|
public limit(limitation: IEndpointMeta['limit'] & { key: NonNullable<string> }, actor: string, factor = 1) {
|
||||||
if (this.disabled) {
|
if (this.disabled) {
|
||||||
|
@ -39,18 +51,12 @@ export class RateLimiterService {
|
||||||
// Short-term limit
|
// Short-term limit
|
||||||
const min = () => {
|
const min = () => {
|
||||||
return new Promise<void>((ok, reject) => {
|
return new Promise<void>((ok, reject) => {
|
||||||
const minIntervalLimiter = new Limiter({
|
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,
|
||||||
});
|
}).then((info) => {
|
||||||
|
|
||||||
minIntervalLimiter.get((err, info) => {
|
|
||||||
if (err) {
|
|
||||||
return reject({ code: 'ERR', info });
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`);
|
this.logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`);
|
||||||
|
|
||||||
if (info.remaining === 0) {
|
if (info.remaining === 0) {
|
||||||
|
@ -69,18 +75,12 @@ export class RateLimiterService {
|
||||||
// Long term limit
|
// Long term limit
|
||||||
const max = () => {
|
const max = () => {
|
||||||
return new Promise<void>((ok, reject) => {
|
return new Promise<void>((ok, reject) => {
|
||||||
const limiter = new Limiter({
|
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,
|
||||||
});
|
}).then((info) => {
|
||||||
|
|
||||||
limiter.get((err, info) => {
|
|
||||||
if (err) {
|
|
||||||
return reject({ code: 'ERR', info });
|
|
||||||
}
|
|
||||||
|
|
||||||
this.logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`);
|
this.logger.debug(`${actor} ${limitation.key} max remaining: ${info.remaining}`);
|
||||||
|
|
||||||
if (info.remaining === 0) {
|
if (info.remaining === 0) {
|
||||||
|
|
Loading…
Reference in New Issue