From 5039b2cadd210a7cadb630329a3324524740c7ce Mon Sep 17 00:00:00 2001 From: haotian Date: Wed, 16 Apr 2025 15:52:39 +0800 Subject: [PATCH 1/3] =?UTF-8?q?update:=20=E5=A2=9E=E5=8A=A0=E5=AF=B9httpCl?= =?UTF-8?q?ient=E7=9A=84=E7=BB=9F=E4=B8=80=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config/config_loader.py | 103 ++-------------- .../config/manage_api_client.py | 116 ++++++++++++++++++ 2 files changed, 127 insertions(+), 92 deletions(-) create mode 100644 main/xiaozhi-server/config/manage_api_client.py diff --git a/main/xiaozhi-server/config/config_loader.py b/main/xiaozhi-server/config/config_loader.py index d71da2d33..49a66558a 100644 --- a/main/xiaozhi-server/config/config_loader.py +++ b/main/xiaozhi-server/config/config_loader.py @@ -1,18 +1,7 @@ import os import argparse -import requests import yaml -import time - - -class DeviceNotFoundException(Exception): - pass - - -class DeviceBindException(Exception): - def __init__(self, bind_code): - self.bind_code = bind_code - super().__init__(f"设备绑定异常,绑定码: {bind_code}") +from config.manage_api_client import init_service, get_server_config, get_agent_models # 添加全局配置缓存 @@ -65,97 +54,27 @@ def get_config_file(): return config_file -def _make_api_request(api_url, secret, endpoint, json_data=None): - """执行API请求的通用函数 - - Args: - api_url: API的基础URL - secret: API密钥 - endpoint: API端点 - json_data: 请求的JSON数据 - - Returns: - dict: API返回的数据 - - Raises: - Exception: 当请求失败时抛出异常 - """ - if not api_url or not secret: - raise Exception("manager-api的url或secret配置错误") - - if "你" in secret: - raise Exception("请先配置manager-api的secret") - - max_retries = 10 - retry_delay = 2 # 秒 - - for attempt in range(max_retries): - try: - response = requests.post(f"{api_url}{endpoint}", json=json_data) - if response.status_code == 200: - result = response.json() - if result.get("code") == 10041: - raise DeviceNotFoundException(result.get("msg")) - elif result.get("code") == 10042: - raise DeviceBindException(result.get("msg")) - elif result.get("code") != 0: - raise Exception(f"API返回错误: {result.get('msg', '未知错误')}") - return result.get("data") - - error_msg = f"manager-api请求失败,状态码: {response.status_code}" - try: - error_data = response.json() - if "msg" in error_data: - error_msg = f"{error_msg}, 错误信息: {error_data['msg']}" - except: - error_msg = f"{error_msg}, 响应内容: {response.text}" - - if attempt < max_retries - 1: - print(f"请求manager-api失败,正在重试 ({attempt + 1}/{max_retries})...") - time.sleep(retry_delay) - else: - raise Exception(error_msg) - - except requests.exceptions.RequestException as e: - if attempt < max_retries - 1: - print(f"请求manager-api异常,正在重试 ({attempt + 1}/{max_retries})...") - time.sleep(retry_delay) - else: - raise Exception(f"manager-api请求异常: {str(e)}") - - def get_config_from_api(config): """从Java API获取配置""" - api_url = config["manager-api"].get("url", "") - secret = config["manager-api"].get("secret", "") + # 初始化API客户端 + init_service(config) + + # 获取服务器配置 + config_data = get_server_config() + if config_data is None: + raise Exception("Failed to fetch server config from API") - config_data = _make_api_request( - api_url, secret, "/config/server-base", {"secret": secret} - ) config_data["read_config_from_api"] = True config_data["manager-api"] = { - "url": api_url, - "secret": secret, + "url": config["manager-api"].get("url", ""), + "secret": config["manager-api"].get("secret", ""), } return config_data def get_private_config_from_api(config, device_id, client_id): """从Java API获取私有配置""" - api_url = config["manager-api"].get("url", "") - secret = config["manager-api"].get("secret", "") - - return _make_api_request( - api_url, - secret, - "/config/agent-models", - { - "secret": secret, - "macAddress": device_id, - "clientId": client_id, - "selectedModule": config["selected_module"], - }, - ) + return get_agent_models(device_id, client_id, config["selected_module"]) def ensure_directories(config): diff --git a/main/xiaozhi-server/config/manage_api_client.py b/main/xiaozhi-server/config/manage_api_client.py new file mode 100644 index 000000000..a27ad95e0 --- /dev/null +++ b/main/xiaozhi-server/config/manage_api_client.py @@ -0,0 +1,116 @@ +import os +from typing import Optional, Dict +import httpx + +TAG = __name__ + + +class DeviceNotFoundException(Exception): + pass + + +class DeviceBindException(Exception): + def __init__(self, bind_code): + self.bind_code = bind_code + super().__init__(f"设备绑定异常,绑定码: {bind_code}") + + +class ManageApiClient: + _instance = None + _client = None + _secret = None + + def __new__(cls, config): + """单例模式确保全局唯一实例,并支持传入配置参数""" + if cls._instance is None: + cls._instance = super().__new__(cls) + cls._init_client(config) + return cls._instance + + @classmethod + def _init_client(cls, config): + """初始化持久化连接池""" + cls.config = config.get('manager-api') + + if not cls.config: + raise Exception("manager-api配置错误") + + if not cls.config.get('url') or not cls.config.get('secret'): + raise Exception("manager-api的url或secret配置错误") + + if "你" in cls.config.get('secret'): + raise Exception("请先配置manager-api的secret") + + cls._secret = cls.config.get('secret') + # NOTE(goody): 2025/4/16 http相关资源统一管理,后续可以增加线程池或者超时 + # 后续也可以统一配置apiToken之类的走通用的Auth + cls._client = httpx.Client( + base_url=cls.config.get('url'), + headers={ + "User-Agent": f"PythonClient/2.0 (PID:{os.getpid()})", + "Accept": "application/json" + }, + ) + + @classmethod + def _execute_request(cls, method: str, endpoint: str, **kwargs) -> Dict: + """增强型请求执行器""" + try: + response = cls._client.request( + method, + endpoint.lstrip("/"), + **kwargs + ) + response.raise_for_status() + result = response.json() + if result.get("code") == 10041: + raise DeviceNotFoundException(result.get("msg")) + elif result.get("code") == 10042: + raise DeviceBindException(result.get("msg")) + elif result.get("code") != 0: + raise Exception(f"API返回错误: {result.get('msg', '未知错误')}") + if result.get("code") == 0: + return result["data"] + return None + except Exception as e: + raise + + @classmethod + def safe_close(cls): + """安全关闭连接池""" + if cls._client: + cls._client.close() + cls._instance = None + + +def get_server_config() -> Optional[Dict]: + """获取服务器基础配置""" + return ManageApiClient._instance._execute_request( + "POST", + "/config/server-base", + json={ + "secret": ManageApiClient._secret + } + ) + + +def get_agent_models(mac_address: str, client_id: str, selected_module: Dict) -> Optional[Dict]: + """获取代理模型配置""" + return ManageApiClient._instance._execute_request( + "POST", + "/config/agent-models", + json={ + "secret": ManageApiClient._secret, + "macAddress": mac_address, + "clientId": client_id, + "selectedModule": selected_module + } + ) + + +def init_service(config): + ManageApiClient(config) + + +def manage_api_http_safe_close(): + ManageApiClient.safe_close() From b62a6933974b1079e1b78e88706edc2d5d6c180f Mon Sep 17 00:00:00 2001 From: GoodyHao <865700600@qq.com> Date: Thu, 17 Apr 2025 00:07:00 +0800 Subject: [PATCH 2/3] =?UTF-8?q?update:=20=E5=A2=9E=E5=8A=A0retry=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/manage_api_client.py | 76 ++++++++++++++----- 1 file changed, 56 insertions(+), 20 deletions(-) diff --git a/main/xiaozhi-server/config/manage_api_client.py b/main/xiaozhi-server/config/manage_api_client.py index a27ad95e0..ed8d94202 100644 --- a/main/xiaozhi-server/config/manage_api_client.py +++ b/main/xiaozhi-server/config/manage_api_client.py @@ -1,5 +1,7 @@ import os +import time from typing import Optional, Dict + import httpx TAG = __name__ @@ -42,6 +44,8 @@ class ManageApiClient: raise Exception("请先配置manager-api的secret") cls._secret = cls.config.get('secret') + cls.max_retries = cls.config.get('max_retries', 3) # 最大重试次数 + cls.retry_delay = cls.config.get('retry_delay', 1) # 初始重试延迟(秒) # NOTE(goody): 2025/4/16 http相关资源统一管理,后续可以增加线程池或者超时 # 后续也可以统一配置apiToken之类的走通用的Auth cls._client = httpx.Client( @@ -50,30 +54,62 @@ class ManageApiClient: "User-Agent": f"PythonClient/2.0 (PID:{os.getpid()})", "Accept": "application/json" }, + timeout=cls.config.get('timeout', 30), # 默认超时时间30秒 ) + @classmethod + def _request(cls, method: str, endpoint: str, **kwargs) -> Dict: + """发送单次HTTP请求并处理响应""" + endpoint = endpoint.lstrip("/") + response = cls._client.request(method, endpoint, **kwargs) + response.raise_for_status() + + result = response.json() + + # 处理API返回的业务错误 + if result.get("code") == 10041: + raise DeviceNotFoundException(result.get("msg")) + elif result.get("code") == 10042: + raise DeviceBindException(result.get("msg")) + elif result.get("code") != 0: + raise Exception(f"API返回错误: {result.get('msg', '未知错误')}") + + # 返回成功数据 + return result.get("data") if result.get("code") == 0 else None + + @classmethod + def _should_retry(cls, exception: Exception) -> bool: + """判断异常是否应该重试""" + # 网络连接相关错误 + if isinstance(exception, (httpx.ConnectError, httpx.TimeoutException, httpx.NetworkError)): + return True + + # HTTP状态码错误 + if isinstance(exception, httpx.HTTPStatusError): + status_code = exception.response.status_code + return status_code in [408, 429, 500, 502, 503, 504] + + return False + @classmethod def _execute_request(cls, method: str, endpoint: str, **kwargs) -> Dict: - """增强型请求执行器""" - try: - response = cls._client.request( - method, - endpoint.lstrip("/"), - **kwargs - ) - response.raise_for_status() - result = response.json() - if result.get("code") == 10041: - raise DeviceNotFoundException(result.get("msg")) - elif result.get("code") == 10042: - raise DeviceBindException(result.get("msg")) - elif result.get("code") != 0: - raise Exception(f"API返回错误: {result.get('msg', '未知错误')}") - if result.get("code") == 0: - return result["data"] - return None - except Exception as e: - raise + """带重试机制的请求执行器""" + retry_count = 0 + + while retry_count <= cls.max_retries: + try: + # 执行请求 + return cls._request(method, endpoint, **kwargs) + except Exception as e: + # 判断是否应该重试 + if retry_count < cls.max_retries and cls._should_retry(e): + retry_count += 1 + print(f"{method} {endpoint} 请求失败,将在 {cls.retry_delay:.1f} 秒后进行第 {retry_count} 次重试") + time.sleep(cls.retry_delay) + continue + else: + # 不重试,直接抛出异常 + raise @classmethod def safe_close(cls): From d5c82cca44ccd20c64e02bf38703d9c367989909 Mon Sep 17 00:00:00 2001 From: GoodyHao <865700600@qq.com> Date: Thu, 17 Apr 2025 00:10:19 +0800 Subject: [PATCH 3/3] =?UTF-8?q?update:=20=E5=A2=9E=E5=8A=A0retry=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config/manage_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/xiaozhi-server/config/manage_api_client.py b/main/xiaozhi-server/config/manage_api_client.py index ed8d94202..2609511d0 100644 --- a/main/xiaozhi-server/config/manage_api_client.py +++ b/main/xiaozhi-server/config/manage_api_client.py @@ -45,7 +45,7 @@ class ManageApiClient: cls._secret = cls.config.get('secret') cls.max_retries = cls.config.get('max_retries', 3) # 最大重试次数 - cls.retry_delay = cls.config.get('retry_delay', 1) # 初始重试延迟(秒) + cls.retry_delay = cls.config.get('retry_delay', 10) # 初始重试延迟(秒) # NOTE(goody): 2025/4/16 http相关资源统一管理,后续可以增加线程池或者超时 # 后续也可以统一配置apiToken之类的走通用的Auth cls._client = httpx.Client(