From 612d4d50827f7e3fa10a3ea2e7a182b33ff2f8fd Mon Sep 17 00:00:00 2001 From: archer <545436317@qq.com> Date: Wed, 10 Dec 2025 15:54:59 +0800 Subject: [PATCH] system qpm limit --- packages/service/common/api/frequencyLimit.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/service/common/api/frequencyLimit.ts b/packages/service/common/api/frequencyLimit.ts index be552d0db..8cc6d24e6 100644 --- a/packages/service/common/api/frequencyLimit.ts +++ b/packages/service/common/api/frequencyLimit.ts @@ -4,6 +4,7 @@ import { jsonRes } from '../../common/response'; import type { NextApiResponse } from 'next'; import { teamQPM } from '../../support/wallet/sub/utils'; import z from 'zod'; +import { addLog } from 'common/system/log'; export enum LimitTypeEnum { chat = 'chat' @@ -31,13 +32,17 @@ const getLimitData = async (data: FrequencyLimitOption) => { return; }; +/* + true: 未达到限制 + false: 达到了限制 +*/ export const teamFrequencyLimit = async ({ teamId, type, res }: FrequencyLimitOption & { res: NextApiResponse }) => { const data = await getLimitData({ type, teamId }); - if (!data) return; + if (!data) return true; const { limit, seconds } = data; @@ -51,13 +56,16 @@ export const teamFrequencyLimit = async ({ .exec(); if (!result) { - return Promise.reject(new Error('Redis connection error')); + return true; } const currentCount = result[0][1] as number; if (currentCount > limit) { const remainingTime = await redis.ttl(key); + addLog.info( + `[Completion Limit] Team ${teamId} reached the limit of ${limit} requests per ${seconds} seconds. Remaining time: ${remainingTime} seconds.` + ); jsonRes(res, { code: 429, error: `Rate limit exceeded. Maximum ${limit} requests per ${seconds} seconds for this team. Please try again in ${remainingTime} seconds.`