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