feat: make redis store optional for rate limiting

Release-As: 2.13.4
This commit is contained in:
Viren070
2025-09-27 16:15:27 +01:00
parent 4f4b04495b
commit b47c293fd3
2 changed files with 12 additions and 7 deletions
+5 -1
View File
@@ -1810,7 +1810,11 @@ export const Env = cleanEnv(process.env, {
default: false,
desc: 'Disable rate limiting',
}),
RATE_LIMIT_STORE: str({
choices: ['memory', 'redis'],
default: 'memory',
desc: 'The store to use for rate limiting',
}),
STATIC_RATE_LIMIT_WINDOW: num({
default: 5, // 1 minute
desc: 'Time window for static file serving rate limiting in seconds',
+7 -6
View File
@@ -21,12 +21,13 @@ const createRateLimiter = (
return (req: Request, res: Response, next: NextFunction) => next();
}
const redisClient = Env.REDIS_URI ? Cache.getRedisClient() : undefined;
const store = redisClient
? new RedisStore({
prefix: `${REDIS_PREFIX}rate-limit:`,
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
})
: new MemoryStore();
const store =
redisClient && Env.RATE_LIMIT_STORE === 'redis'
? new RedisStore({
prefix: `${REDIS_PREFIX}rate-limit:`,
sendCommand: (...args: string[]) => redisClient.sendCommand(args),
})
: new MemoryStore();
return rateLimit({
windowMs,
max: maxRequests,