mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-25 19:01:06 +03:00
Merge pull request #957 from FlowiseAI/feature/RateLimit
Chore/RateLimit
This commit is contained in:
@@ -27,6 +27,7 @@ export default class Start extends Command {
|
||||
LOG_LEVEL: Flags.string(),
|
||||
TOOL_FUNCTION_BUILTIN_DEP: Flags.string(),
|
||||
TOOL_FUNCTION_EXTERNAL_DEP: Flags.string(),
|
||||
NUMBER_OF_PROXIES: Flags.string(),
|
||||
DATABASE_TYPE: Flags.string(),
|
||||
DATABASE_PATH: Flags.string(),
|
||||
DATABASE_PORT: Flags.string(),
|
||||
@@ -72,6 +73,7 @@ export default class Start extends Command {
|
||||
|
||||
if (flags.PORT) process.env.PORT = flags.PORT
|
||||
if (flags.DEBUG) process.env.DEBUG = flags.DEBUG
|
||||
if (flags.NUMBER_OF_PROXIES) process.env.NUMBER_OF_PROXIES = flags.NUMBER_OF_PROXIES
|
||||
|
||||
// Authorization
|
||||
if (flags.FLOWISE_USERNAME) process.env.FLOWISE_USERNAME = flags.FLOWISE_USERNAME
|
||||
|
||||
@@ -12,7 +12,7 @@ async function addRateLimiter(id: string, duration: number, limit: number, messa
|
||||
rateLimiters[id] = rateLimit({
|
||||
windowMs: duration * 1000,
|
||||
max: limit,
|
||||
handler: (req, res) => {
|
||||
handler: (_, res) => {
|
||||
res.status(429).send(message)
|
||||
}
|
||||
})
|
||||
@@ -33,15 +33,19 @@ export function getRateLimiter(req: Request, res: Response, next: NextFunction)
|
||||
|
||||
export async function createRateLimiter(chatFlow: IChatFlow) {
|
||||
if (!chatFlow.apiConfig) return
|
||||
const apiConfig: any = JSON.parse(chatFlow.apiConfig)
|
||||
const apiConfig = JSON.parse(chatFlow.apiConfig)
|
||||
|
||||
const rateLimit: { limitDuration: number; limitMax: number; limitMsg: string } = apiConfig.rateLimit
|
||||
if (!rateLimit) return
|
||||
|
||||
const { limitDuration, limitMax, limitMsg } = rateLimit
|
||||
if (limitMax && limitDuration && limitMsg) await addRateLimiter(chatFlow.id, limitDuration, limitMax, limitMsg)
|
||||
}
|
||||
|
||||
export async function initializeRateLimiter(chatFlowPool: IChatFlow[]) {
|
||||
await chatFlowPool.map(async (chatFlow) => {
|
||||
await createRateLimiter(chatFlow)
|
||||
})
|
||||
await Promise.all(
|
||||
chatFlowPool.map(async (chatFlow) => {
|
||||
await createRateLimiter(chatFlow)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user