mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 19:00:59 +03:00
modify rateLimit data storage
This commit is contained in:
@@ -15,9 +15,7 @@ export interface IChatFlow {
|
|||||||
isPublic?: boolean
|
isPublic?: boolean
|
||||||
apikeyid?: string
|
apikeyid?: string
|
||||||
chatbotConfig?: string
|
chatbotConfig?: string
|
||||||
rateLimit?: number
|
apiConfig?: any
|
||||||
rateLimitDuration?: number
|
|
||||||
rateLimitMsg?: string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IChatMessage {
|
export interface IChatMessage {
|
||||||
|
|||||||
@@ -26,13 +26,7 @@ export class ChatFlow implements IChatFlow {
|
|||||||
chatbotConfig?: string
|
chatbotConfig?: string
|
||||||
|
|
||||||
@Column({ nullable: true })
|
@Column({ nullable: true })
|
||||||
rateLimit?: number
|
apiConfig?: string
|
||||||
|
|
||||||
@Column({ nullable: true })
|
|
||||||
rateLimitDuration?: number
|
|
||||||
|
|
||||||
@Column({ nullable: true })
|
|
||||||
rateLimitMsg?: string
|
|
||||||
|
|
||||||
@CreateDateColumn()
|
@CreateDateColumn()
|
||||||
createdDate: Date
|
createdDate: Date
|
||||||
|
|||||||
@@ -319,6 +319,9 @@ export class App {
|
|||||||
const updateChatFlow = new ChatFlow()
|
const updateChatFlow = new ChatFlow()
|
||||||
Object.assign(updateChatFlow, body)
|
Object.assign(updateChatFlow, body)
|
||||||
|
|
||||||
|
updateChatFlow.id = chatflow.id
|
||||||
|
createRateLimiter(updateChatFlow)
|
||||||
|
|
||||||
this.AppDataSource.getRepository(ChatFlow).merge(chatflow, updateChatFlow)
|
this.AppDataSource.getRepository(ChatFlow).merge(chatflow, updateChatFlow)
|
||||||
const result = await this.AppDataSource.getRepository(ChatFlow).save(chatflow)
|
const result = await this.AppDataSource.getRepository(ChatFlow).save(chatflow)
|
||||||
|
|
||||||
@@ -762,38 +765,6 @@ export class App {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// ----------------------------------------
|
|
||||||
// Rate Limit
|
|
||||||
// ----------------------------------------
|
|
||||||
|
|
||||||
this.app.get(
|
|
||||||
'/api/v1/rate-limit/:id',
|
|
||||||
upload.array('files'),
|
|
||||||
(req: Request, res: Response, next: NextFunction) => getRateLimiter(req, res, next),
|
|
||||||
async (req: Request, res: Response) => {
|
|
||||||
res.send("you're fine")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
this.app.post('/api/v1/rate-limit/', async (req: Request, res: Response) => {
|
|
||||||
const id = req.body.id
|
|
||||||
const duration = req.body.duration
|
|
||||||
const limit = req.body.limit
|
|
||||||
const message = req.body.message
|
|
||||||
|
|
||||||
const result = await getDataSource()
|
|
||||||
.getRepository(ChatFlow)
|
|
||||||
.createQueryBuilder()
|
|
||||||
.update(ChatFlow)
|
|
||||||
.set({ rateLimit: limit, rateLimitDuration: duration, rateLimitMsg: message })
|
|
||||||
.where('id = :id', { id: id })
|
|
||||||
.execute()
|
|
||||||
|
|
||||||
await createRateLimiter(id, Number(duration), Number(limit), message)
|
|
||||||
|
|
||||||
res.send({ result })
|
|
||||||
})
|
|
||||||
|
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
// Serve UI static
|
// Serve UI static
|
||||||
// ----------------------------------------
|
// ----------------------------------------
|
||||||
|
|||||||
@@ -6,11 +6,11 @@ import { Mutex } from 'async-mutex'
|
|||||||
let rateLimiters: Record<string, RateLimitRequestHandler> = {}
|
let rateLimiters: Record<string, RateLimitRequestHandler> = {}
|
||||||
const rateLimiterMutex = new Mutex()
|
const rateLimiterMutex = new Mutex()
|
||||||
|
|
||||||
export async function createRateLimiter(id: string, duration: number, limit: number, message: string) {
|
async function addRateLimiter(id: string, duration: number, limit: number, message: string) {
|
||||||
const release = await rateLimiterMutex.acquire()
|
const release = await rateLimiterMutex.acquire()
|
||||||
try {
|
try {
|
||||||
rateLimiters[id] = rateLimit({
|
rateLimiters[id] = rateLimit({
|
||||||
windowMs: duration,
|
windowMs: duration * 1000,
|
||||||
max: limit,
|
max: limit,
|
||||||
handler: (req, res) => {
|
handler: (req, res) => {
|
||||||
res.status(429).send(message)
|
res.status(429).send(message)
|
||||||
@@ -31,9 +31,17 @@ export function getRateLimiter(req: Request, res: Response, next: NextFunction)
|
|||||||
return idRateLimiter(req, res, next)
|
return idRateLimiter(req, res, next)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function initializeRateLimiter(ChatFlowPool: IChatFlow[]) {
|
export async function createRateLimiter(chatFlow: IChatFlow) {
|
||||||
await ChatFlowPool.map(async (ChatFlow) => {
|
if (!chatFlow.apiConfig) return
|
||||||
if (ChatFlow.rateLimitDuration && ChatFlow.rateLimit && ChatFlow.rateLimitMsg)
|
const apiConfig: any = JSON.parse(chatFlow.apiConfig)
|
||||||
await createRateLimiter(ChatFlow.id, ChatFlow.rateLimitDuration, ChatFlow.rateLimit, ChatFlow.rateLimitMsg)
|
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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,8 +127,8 @@ const Configuration = () => {
|
|||||||
<Typography variant='h4' sx={{ mb: 1, mt: 2 }}>
|
<Typography variant='h4' sx={{ mb: 1, mt: 2 }}>
|
||||||
Rate Limit
|
Rate Limit
|
||||||
</Typography>
|
</Typography>
|
||||||
{textField(limitMax, 'Limit Max', 'Max Limit', 'number')}
|
{textField(limitMax, 'limitMax', 'Message Limit per Duration', 'number')}
|
||||||
{textField(limitDuration, 'Limit Duration', 'Font Size', 'number')}
|
{textField(limitDuration, 'limitDuration', 'Duration in Second', 'number')}
|
||||||
{textField(limitMsg, 'limitMsg', 'Limit Message', 'string')}
|
{textField(limitMsg, 'limitMsg', 'Limit Message', 'string')}
|
||||||
|
|
||||||
<StyledButton style={{ marginBottom: 10, marginTop: 10 }} variant='contained' onClick={() => onSave()}>
|
<StyledButton style={{ marginBottom: 10, marginTop: 10 }} variant='contained' onClick={() => onSave()}>
|
||||||
|
|||||||
Reference in New Issue
Block a user