use redis store in rate limiters

This commit is contained in:
miloschwartz 2025-06-22 19:55:05 -04:00
parent 0e87b6e48b
commit fb5010a2b5
No known key found for this signature in database
3 changed files with 17 additions and 28 deletions

View file

@ -7,13 +7,16 @@ import config from "@server/lib/config";
import { RedisStore } from "rate-limit-redis";
import redisManager from "@server/db/redis";
export let rateLimitStore: Store = new MemoryStore();
if (config.getRawConfig().flags?.enable_redis) {
const client = redisManager.client!;
rateLimitStore = new RedisStore({
sendCommand: async (command: string, ...args: string[]) =>
(await client.call(command, args)) as any
});
export function createStore(): Store {
let rateLimitStore: Store = new MemoryStore();
if (config.getRawConfig().flags?.enable_redis) {
const client = redisManager.client!;
rateLimitStore = new RedisStore({
sendCommand: async (command: string, ...args: string[]) =>
(await client.call(command, args)) as any
});
}
return rateLimitStore;
}
export function rateLimitMiddleware({
@ -44,7 +47,7 @@ export function rateLimitMiddleware({
createHttpError(HttpCode.TOO_MANY_REQUESTS, message)
);
},
store: rateLimitStore
store: createStore()
});
} else {
return rateLimit({
@ -57,7 +60,8 @@ export function rateLimitMiddleware({
return next(
createHttpError(HttpCode.TOO_MANY_REQUESTS, message)
);
}
},
store: createStore()
});
}
}