From 5e0d30dde8044cbcf22f106a0a947c562925baeb Mon Sep 17 00:00:00 2001 From: wwq Date: Tue, 21 Apr 2026 21:16:35 +0800 Subject: [PATCH] fix(quota): restrict quota check to new terminal user creation only - Avoid redundant quota checks for existing users on every request to optimize performance. --- api/app/controllers/service/app_api_controller.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/api/app/controllers/service/app_api_controller.py b/api/app/controllers/service/app_api_controller.py index a78fd842..d8aefc72 100644 --- a/api/app/controllers/service/app_api_controller.py +++ b/api/app/controllers/service/app_api_controller.py @@ -106,6 +106,16 @@ async def chat( other_id = payload.user_id workspace_id = api_key_auth.workspace_id end_user_repo = EndUserRepository(db) + + # 仅在新建终端用户时检查配额,已有用户复用不受限制 + existing_end_user = end_user_repo.get_end_user_by_other_id(workspace_id=workspace_id, other_id=other_id) + if existing_end_user is None: + from app.core.quota_manager import _check_quota + from app.models.workspace_model import Workspace + ws = db.query(Workspace).filter(Workspace.id == workspace_id).first() + if ws: + _check_quota(db, ws.tenant_id, "end_user_quota", "end_user") + new_end_user = end_user_repo.get_or_create_end_user( app_id=app.id, workspace_id=workspace_id,