From d59990d3260652a88d55773b47ae2e26e8a713cf Mon Sep 17 00:00:00 2001 From: wwq Date: Mon, 20 Apr 2026 18:25:39 +0800 Subject: [PATCH] fix(rate_limit): differentiate between tenant plan and API Key QPS limit errors - Add logic to detect tenant plan QPS limits and return a specific error message when triggered. - Simplify boolean check in model activation quota validation. --- api/app/core/api_key_auth.py | 2 ++ api/app/core/quota_manager.py | 4 ++-- api/app/services/api_key_service.py | 7 ++++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api/app/core/api_key_auth.py b/api/app/core/api_key_auth.py index 1ded6f81..05bca945 100644 --- a/api/app/core/api_key_auth.py +++ b/api/app/core/api_key_auth.py @@ -108,6 +108,8 @@ def require_api_key( # 根据错误消息判断限流类型 if "Daily" in error_msg: code = BizCode.API_KEY_DAILY_LIMIT_EXCEEDED + elif "Tenant" in error_msg: + code = BizCode.API_KEY_QPS_LIMIT_EXCEEDED # 租户套餐速率超限,同属 QPS 类 elif "QPS" in error_msg: code = BizCode.API_KEY_QPS_LIMIT_EXCEEDED else: diff --git a/api/app/core/quota_manager.py b/api/app/core/quota_manager.py index bf04059e..534e1940 100644 --- a/api/app/core/quota_manager.py +++ b/api/app/core/quota_manager.py @@ -488,7 +488,7 @@ def check_model_activation_quota(func: Callable) -> Callable: logger.warning("模型激活配额检查失败:缺少 model_id 或 model_data 参数") return await func(*args, **kwargs) - if model_data.is_active is True: + if model_data.is_active: try: from app.services.model_service import ModelConfigService @@ -522,7 +522,7 @@ def check_model_activation_quota(func: Callable) -> Callable: logger.warning("模型激活配额检查失败:缺少 model_id 或 model_data 参数") return func(*args, **kwargs) - if model_data.is_active is True: + if model_data.is_active: try: from app.services.model_service import ModelConfigService diff --git a/api/app/services/api_key_service.py b/api/app/services/api_key_service.py index e67d623e..5595e93f 100644 --- a/api/app/services/api_key_service.py +++ b/api/app/services/api_key_service.py @@ -386,7 +386,12 @@ class RateLimiterService: # 用最终有效限额做 QPS 检查 qps_ok, qps_info = await self.check_qps(api_key.id, effective_limit) if not qps_ok: - return False, "QPS limit exceeded", { + # 判断是套餐限额触发还是 api_key 自身限额触发 + if tenant_limit and effective_limit == tenant_limit and api_key.rate_limit > tenant_limit: + error_msg = "Tenant QPS limit exceeded" + else: + error_msg = "QPS limit exceeded" + return False, error_msg, { "X-RateLimit-Limit-QPS": str(qps_info["limit"]), "X-RateLimit-Remaining-QPS": str(qps_info["remaining"]), "X-RateLimit-Reset": str(qps_info["reset"])