mirror of
https://github.com/Viren070/AIOStreams.git
synced 2025-12-01 23:14:04 +01:00
feat: make redis store optional for rate limiting
Release-As: 2.13.4
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user