mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-31 16:14:46 +02:00
use redis store in rate limiters
This commit is contained in:
parent
0e87b6e48b
commit
fb5010a2b5
3 changed files with 17 additions and 28 deletions
|
@ -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()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue