perf(api): reduce workflow startup latency for chatflow (#36773)

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
呆萌闷油瓶
2026-06-11 07:05:35 +00:00
committed by GitHub
co-authored by autofix-ci[bot]
parent 632df88228
commit c4a8d79be9
7 changed files with 620 additions and 99 deletions
+30 -6
View File
@@ -4,6 +4,7 @@ from copy import deepcopy
from typing import Any
from core.app.entities.app_invoke_entities import DifyRunContext, ModelConfigWithCredentialsEntity
from core.entities.model_entities import ModelWithProviderEntity
from core.errors.error import ProviderTokenNotInitError
from core.model_manager import ModelInstance, ModelManager
from core.plugin.impl.model_runtime_factory import create_plugin_provider_manager
@@ -19,6 +20,10 @@ class DifyCredentialsProvider:
Fetched credentials are stored in :attr:`credentials_cache` and reused for
subsequent ``fetch`` calls for the same ``(provider_name, model_name)``.
The matching validated provider model is cached alongside the credentials so
follow-up workflow startup checks can reuse the earlier provider lookup
instead of resolving the same model metadata a second time.
Because of that cache, a single instance can return stale credentials after
the tenant or provider configuration changes (e.g. API key rotation).
@@ -30,6 +35,7 @@ class DifyCredentialsProvider:
tenant_id: str
provider_manager: ProviderManager
credentials_cache: dict[tuple[str, str], dict[str, Any]]
provider_model_cache: dict[tuple[str, str], ModelWithProviderEntity]
def __init__(
self,
@@ -45,12 +51,21 @@ class DifyCredentialsProvider:
)
self.provider_manager = provider_manager
self.credentials_cache = {}
self.provider_model_cache = {}
def get_cached_provider_model(self, provider_name: str, model_name: str) -> ModelWithProviderEntity | None:
provider_model = self.provider_model_cache.get((provider_name, model_name))
if provider_model is None:
return None
return provider_model.model_copy(deep=True)
def fetch(self, provider_name: str, model_name: str) -> dict[str, Any]:
if (provider_name, model_name) in self.credentials_cache:
return deepcopy(self.credentials_cache[(provider_name, model_name)])
provider_configurations = self.provider_manager.get_configurations(self.tenant_id)
provider_configuration = provider_configurations.get(provider_name)
if not provider_configuration:
raise ValueError(f"Provider {provider_name} does not exist.")
@@ -65,6 +80,7 @@ class DifyCredentialsProvider:
raise ProviderTokenNotInitError(f"Model {model_name} credentials is not initialized.")
self.credentials_cache[(provider_name, model_name)] = deepcopy(credentials)
self.provider_model_cache[(provider_name, model_name)] = provider_model.model_copy(deep=True)
return credentials
@@ -142,13 +158,21 @@ def fetch_model_config(
model_instance = model_factory.init_model_instance(node_data_model.provider, node_data_model.name)
provider_model_bundle = model_instance.provider_model_bundle
provider_model = provider_model_bundle.configuration.get_provider_model(
model=node_data_model.name,
model_type=ModelType.LLM,
)
provider_model = None
if isinstance(credentials_provider, DifyCredentialsProvider):
provider_model = credentials_provider.get_cached_provider_model(
provider_name=node_data_model.provider,
model_name=node_data_model.name,
)
if provider_model is None:
raise ModelNotExistError(f"Model {node_data_model.name} does not exist.")
provider_model.raise_for_status()
provider_model = provider_model_bundle.configuration.get_provider_model(
model=node_data_model.name,
model_type=ModelType.LLM,
)
if provider_model is None:
raise ModelNotExistError(f"Model {node_data_model.name} does not exist.")
provider_model.raise_for_status()
model_schema = model_instance.model_type_instance.get_model_schema(node_data_model.name, credentials)
if model_schema is None: