mirror of
https://github.com/labring/FastGPT.git
synced 2025-12-25 20:02:47 +00:00
api entry qpm
This commit is contained in:
parent
036c37855f
commit
1210c07217
|
|
@ -46,7 +46,7 @@ export const getTeamQPMLimit = async (teamId: string): Promise<number> => {
|
|||
|
||||
export const teamFrequencyLimit = async ({ teamId, type, res }: FrequencyLimitOption) => {
|
||||
const limit = await getTeamQPMLimit(teamId);
|
||||
const seconds = limitSecondsMap[type];
|
||||
const seconds = limitSecondsMap[type] ?? 60;
|
||||
|
||||
const redis = getGlobalRedisConnection();
|
||||
const key = `frequency:${type}:${teamId}`;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ import { authFrequencyLimit } from '../system/frequencyLimit/utils';
|
|||
import { addSeconds } from 'date-fns';
|
||||
import { type NextApiResponse } from 'next';
|
||||
import { jsonRes } from '../response';
|
||||
import { authCert } from '../../support/permission/auth/common';
|
||||
import { teamFrequencyLimit } from '../api/frequencyLimit';
|
||||
|
||||
// unit: times/s
|
||||
// how to use?
|
||||
|
|
@ -38,3 +40,34 @@ export function useIPFrequencyLimit({
|
|||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function useTeamFrequencyLimit({
|
||||
paths = ['/api/v', '/api/core/', '/api/support/']
|
||||
}: {
|
||||
paths?: string[];
|
||||
} = {}) {
|
||||
return async (req: ApiRequestProps, res: NextApiResponse) => {
|
||||
const isTargetPath = paths.some((path) => req.url?.startsWith(path));
|
||||
if (!isTargetPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const { teamId } = await authCert({
|
||||
req,
|
||||
authToken: true,
|
||||
authApiKey: true
|
||||
});
|
||||
|
||||
if (teamId) {
|
||||
await teamFrequencyLimit({
|
||||
teamId,
|
||||
type: req.url as any,
|
||||
res
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
return;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,16 +98,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||
per: ReadPermissionVal
|
||||
});
|
||||
|
||||
if (
|
||||
!(await teamFrequencyLimit({
|
||||
teamId,
|
||||
type: LimitTypeEnum.chat,
|
||||
res
|
||||
}))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
pushTrack.teamChatQPM({ teamId });
|
||||
|
||||
const isPlugin = app.type === AppTypeEnum.workflowTool;
|
||||
|
|
|
|||
|
|
@ -189,16 +189,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||
});
|
||||
})();
|
||||
|
||||
if (
|
||||
!(await teamFrequencyLimit({
|
||||
teamId,
|
||||
type: LimitTypeEnum.chat,
|
||||
res
|
||||
}))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
pushTrack.teamChatQPM({ teamId });
|
||||
|
||||
retainDatasetCite = retainDatasetCite && !!responseDetail;
|
||||
|
|
|
|||
|
|
@ -190,16 +190,6 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|||
});
|
||||
})();
|
||||
|
||||
if (
|
||||
!(await teamFrequencyLimit({
|
||||
teamId,
|
||||
type: LimitTypeEnum.chat,
|
||||
res
|
||||
}))
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
pushTrack.teamChatQPM({ teamId });
|
||||
|
||||
retainDatasetCite = retainDatasetCite && !!responseDetail;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,6 @@
|
|||
import { NextEntry } from '@fastgpt/service/common/middle/entry';
|
||||
import { useTeamFrequencyLimit } from '@fastgpt/service/common/middle/reqFrequencyLimit';
|
||||
|
||||
export const NextAPI = NextEntry({ beforeCallback: [] });
|
||||
export const NextAPI = NextEntry({
|
||||
beforeCallback: [useTeamFrequencyLimit()]
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue