From 3eb187580459e296afc77a477844d4f0f83c76aa Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 14 Jun 2024 17:41:39 +0900 Subject: [PATCH] refactor: check if there is rate limit inside min/max function --- .../src/server/api/RateLimiterService.ts | 70 ++++++++----------- 1 file changed, 30 insertions(+), 40 deletions(-) diff --git a/packages/backend/src/server/api/RateLimiterService.ts b/packages/backend/src/server/api/RateLimiterService.ts index 9c125dc0df..7d782795f9 100644 --- a/packages/backend/src/server/api/RateLimiterService.ts +++ b/packages/backend/src/server/api/RateLimiterService.ts @@ -50,53 +50,43 @@ export class RateLimiterService { // Short-term limit const min = async () => { - const info = await this.checkLimiter({ - id: `${actor}:${limitation.key}:min`, - duration: limitation.minInterval! * factor, - max: 1, - db: this.redisClient, - }); - - this.logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`); - - if (info.remaining === 0) { - // eslint-disable-next-line no-throw-literal - throw { code: 'BRIEF_REQUEST_INTERVAL', info }; - } else { - return; + if (limitation.minInterval != null) { + const info = await this.checkLimiter({ + id: `${actor}:${limitation.key}:min`, + duration: limitation.minInterval * factor, + max: 1, + db: this.redisClient, + }); + this.logger.debug(`${actor} ${limitation.key} min remaining: ${info.remaining}`); + if (info.remaining === 0) { + // eslint-disable-next-line no-throw-literal + throw { code: 'BRIEF_REQUEST_INTERVAL', info }; + } else { + return; + } } }; // Long term limit const max = async () => { - const info = await this.checkLimiter({ - id: `${actor}:${limitation.key}`, - duration: limitation.duration! * factor, - max: limitation.max! / factor, - 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 }; - } else { - return; + if (limitation.duration != null && limitation.max != null) { + const info = await this.checkLimiter({ + id: `${actor}:${limitation.key}`, + duration: limitation.duration * factor, + max: limitation.max / factor, + 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 }; + } else { + return; + } } }; - const hasShortTermLimit = typeof limitation.minInterval === 'number'; - - const hasLongTermLimit = - typeof limitation.duration === 'number' && - typeof limitation.max === 'number'; - - if (hasShortTermLimit) { - await min(); - } - if (hasLongTermLimit) { - await max(); - } + await min(); + await max(); } }