瀏覽代碼

opt(api): optimize update contention on the providers table (#24520)

tags/1.8.0
QuantumGhost 2 月之前
父節點
當前提交
410fe7293f
No account linked to committer's email address
共有 1 個文件被更改,包括 8 次插入2 次删除
  1. 8
    2
      api/events/event_handlers/update_provider_when_message_created.py

+ 8
- 2
api/events/event_handlers/update_provider_when_message_created.py 查看文件

@@ -85,6 +85,7 @@ def handle(sender: Message, **kwargs):
values=_ProviderUpdateValues(last_used=current_time),
description="basic_last_used_update",
)
logging.info("provider used, tenant_id=%s, provider_name=%s", tenant_id, provider_name)
updates_to_perform.append(basic_update)

# 2. Check if we need to deduct quota (system provider only)
@@ -186,6 +187,8 @@ def _execute_provider_updates(updates_to_perform: list[_ProviderUpdateOperation]
if not updates_to_perform:
return

updates_to_perform = sorted(updates_to_perform, key=lambda i: (i.filters.tenant_id, i.filters.provider_name))

# Use SQLAlchemy's context manager for transaction management
# This automatically handles commit/rollback
with Session(db.engine) as session, session.begin():
@@ -212,10 +215,13 @@ def _execute_provider_updates(updates_to_perform: list[_ProviderUpdateOperation]

# Prepare values dict for SQLAlchemy update
update_values = {}
if values.last_used is not None:
update_values["last_used"] = values.last_used
# updateing to `last_used` is removed due to performance reason.
# ref: https://github.com/langgenius/dify/issues/24526
if values.quota_used is not None:
update_values["quota_used"] = values.quota_used
# Skip the current update operation if no updates are required.
if not update_values:
continue

# Build and execute the update statement
stmt = update(Provider).where(*where_conditions).values(**update_values)

Loading…
取消
儲存