api entry qpm

This commit is contained in:
heheer 2025-12-09 17:52:55 +08:00
parent 036c37855f
commit 1210c07217
No known key found for this signature in database
GPG Key ID: 37DCB43201661540
6 changed files with 38 additions and 32 deletions

View File

@ -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}`;

View File

@ -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;
}
};
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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()]
});