JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
perf(agentserver/gateway): 合并配置 mtime 缓存 + Pod 通道 per-pod 锁
wangmingqi1a22c686f268
diff --git a/jiuwenswarm/common/config.py b/jiuwenswarm/common/config.pyindex a2a2b16d02..fedd3c728f 100644--- a/jiuwenswarm/common/config.py+++ b/jiuwenswarm/common/config.py@@ -62,11 +62,47 @@ def _current_config_yaml_path() -> Path:return get_config_file()+_MERGED_CONFIG_CACHE: dict[str, Any] | None = None+_MERGED_CONFIG_CACHE_KEY: tuple | None = None+++def invalidate_merged_config_cache() -> None:+ """[PERF] 配置写入/失效路径调用:清合并配置缓存。"""+ global _MERGED_CONFIG_CACHE, _MERGED_CONFIG_CACHE_KEY+ _MERGED_CONFIG_CACHE = None+ _MERGED_CONFIG_CACHE_KEY = None++def get_merged_config_dict() -> dict[str, Any]:- """模板与用户 override 合并后的字典(不解析环境变量)。"""- template = load_yaml_dict(resolve_shipped_template_config_path())- override = load_yaml_dict(_current_config_yaml_path())- return merge_template_with_override(template, override)+ """模板与用户 override 合并后的字典(不解析环境变量)。++ [PERF] 实测双 YAML 重读+合并 ~150ms,且经 RuntimePromptRail 的+ 模型名兜底路径被**每次模型调用**触发(每次聊天多付 ~150ms)。按+ (路径, mtime_ns) 缓存合并结果:文件未变直接复用;配置写入方走+ invalidate_merged_config_cache() 主动失效(写文件本身也会变 mtime,+ 双保险)。注意:调用方不应修改返回的 dict(共享缓存对象)。+ """+ global _MERGED_CONFIG_CACHE, _MERGED_CONFIG_CACHE_KEY+ tpl_path = resolve_shipped_template_config_path()+ usr_path = _current_config_yaml_path()+ try:+ key = (+ str(tpl_path),+ tpl_path.stat().st_mtime_ns if tpl_path.exists() else None,+ str(usr_path),+ usr_path.stat().st_mtime_ns if usr_path.exists() else None,+ )+ except OSError:+ key = None+ if key is not None and key == _MERGED_CONFIG_CACHE_KEY and _MERGED_CONFIG_CACHE is not None:+ return _MERGED_CONFIG_CACHE+ template = load_yaml_dict(tpl_path)+ override = load_yaml_dict(usr_path)+ merged = merge_template_with_override(template, override)+ if key is not None:+ _MERGED_CONFIG_CACHE_KEY = key+ _MERGED_CONFIG_CACHE = merged+ return mergeddef resolve_env_vars(value: Any) -> Any:@@ -376,6 +412,7 @@ def clear_config_cache(]for key in keys_to_remove:_resolved_config_by_ns.pop(key, None)+ invalidate_merged_config_cache()if sid == "default" and aid == "default":_resolved_config_by_ns.pop(None, None)unbound_overlay_keys = [diff --git a/packages/jiuwenclaw-ee/gateway/extensions/runtime_management_extension/runtime_routed_client.py b/packages/jiuwenclaw-ee/gateway/extensions/runtime_management_extension/runtime_routed_client.pyindex 8f5385e8f7..256a37818c 100644--- a/packages/jiuwenclaw-ee/gateway/extensions/runtime_management_extension/runtime_routed_client.py+++ b/packages/jiuwenclaw-ee/gateway/extensions/runtime_management_extension/runtime_routed_client.py@@ -202,7 +202,9 @@ class RuntimeRoutedAgentClient(AgentServerClient):self._connected = Falseself._on_server_push: Callable[[dict[str, Any]], Awaitable[None]] | None = Noneself._pod_clients: dict[str, HttpSseAgentServerClient] = {}- self._pod_connect_lock = asyncio.Lock()+ # [PERF] 全局连接锁改 per-pod:10 个冷 Pod 首波建连由串行 ~0.3s×N 变并行 ~0.3s+ self._pod_connect_locks: dict[str, asyncio.Lock] = {}+ self._pod_locks_guard = asyncio.Lock()self._rpc_origins: dict[tuple[str, str], str] = {}self._drop_tasks: set[asyncio.Task[None]] = set()self._last_rebind_warn = 0.0 # rebind 降级告警节流(滚动期旧 runtime 404 防刷屏)@@ -232,11 +234,19 @@ class RuntimeRoutedAgentClient(AgentServerClient):self._rpc_origins.pop(key, None)raise+ async def _pod_lock_for(self, base_url: str) -> asyncio.Lock:+ """[PERF] 按 Pod 取连接锁:不同 Pod 的建连互不阻塞。"""+ async with self._pod_locks_guard:+ lock = self._pod_connect_locks.get(base_url)+ if lock is None:+ lock = self._pod_connect_locks[base_url] = asyncio.Lock()+ return lock+async def _ensure_pod_push(self, base_url: str) -> None:if self._on_server_push is None:returnstale: HttpSseAgentServerClient | None = None- async with self._pod_connect_lock:+ async with await self._pod_lock_for(base_url):self._ensure_connected()client = self._pod_clients.get(base_url)# Fallback if push-done drop hasn't run yet (same IP reused).@@ -289,7 +299,7 @@ class RuntimeRoutedAgentClient(AgentServerClient):async def _drop_pod_client(self, base_url: str, client: HttpSseAgentServerClient) -> None:- async with self._pod_connect_lock:+ async with await self._pod_lock_for(base_url):if self._pod_clients.get(base_url) is not client:returnself._pod_clients.pop(base_url, None)@@ -306,6 +316,7 @@ class RuntimeRoutedAgentClient(AgentServerClient):exc,)+async def connect(self, uri: str) -> None:_ = uriself._connected = True@@ -317,9 +328,10 @@ class RuntimeRoutedAgentClient(AgentServerClient):async def disconnect(self) -> None:self._connected = False- async with self._pod_connect_lock:+ async with self._pod_locks_guard:clients = list(self._pod_clients.values())self._pod_clients.clear()+ self._pod_connect_locks.clear()drop_tasks = list(self._drop_tasks)self._drop_tasks.clear()if drop_tasks: