Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
opt(api): optimize update contention on the providers table (#24520)
QuantumGhost410fe7293fbe
diff --git a/api/events/event_handlers/update_provider_when_message_created.py b/api/events/event_handlers/update_provider_when_message_created.pyindex f01dd589003..0ae6d9ee0d9 100644--- a/api/events/event_handlers/update_provider_when_message_created.py+++ b/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/rollbackwith 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 updateupdate_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/24526if 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 statementstmt = update(Provider).where(*where_conditions).values(**update_values)