From 0ef7f14fa9a43e38a2339a1e36d817edf2fb67b0 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 14 Jun 2024 17:48:05 +0900 Subject: [PATCH] refactor: remove unnecessary max/min function --- .../src/server/api/RateLimiterService.ts | 59 +++++++++---------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/packages/backend/src/server/api/RateLimiterService.ts b/packages/backend/src/server/api/RateLimiterService.ts index 8230c98c1d..f78b380d9e 100644 --- a/packages/backend/src/server/api/RateLimiterService.ts +++ b/packages/backend/src/server/api/RateLimiterService.ts @@ -49,40 +49,37 @@ export class RateLimiterService { } // Short-term limit - const min = async () => { - 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 }; - } + 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 }; } - }; + } // Long term limit - const max = async () => { - 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 }; - } - } - }; + 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, + }); - await min(); - await max(); + 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 }; + } + } } }