refactor(managers): 重构单例管理器实现并优化代码结构

feat(ws_pool): 新增 WebSocket 连接池实现

perf(json): 使用 orjson 替代标准 json 库提升性能

style: 清理未使用的导入和冗余代码

docs: 更新架构文档和开发规范

test: 添加 WebSocket 连接池测试用例

fix(plugins): 修复自动审批插件 API 调用参数格式
This commit is contained in:
2026-01-22 16:23:03 +08:00
parent d7d732ff4d
commit caf5b06097
42 changed files with 1285 additions and 261 deletions

View File

@@ -6,7 +6,6 @@
# 导出核心工具
from .logger import logger
from .exceptions import *
from .json_utils import *
from .singleton import singleton
from .executor import run_in_thread_pool, initialize_executor
from .performance import (

View File

@@ -3,6 +3,7 @@
该模块定义了项目中使用的错误码和统一的错误响应格式,确保所有模块返回一致的错误信息。
"""
from typing import Optional
# 错误码定义
class ErrorCode:
@@ -142,7 +143,7 @@ def get_error_message(code: int) -> str:
return ERROR_MESSAGES.get(code, ERROR_MESSAGES[ErrorCode.UNKNOWN_ERROR])
def create_error_response(code: int, message: str = None, data: dict = None, request_id: str = None) -> dict:
def create_error_response(code: int, message: Optional[str] = None, data: Optional[dict] = None, request_id: Optional[str] = None) -> dict:
"""
创建统一格式的错误响应
@@ -172,7 +173,7 @@ def create_error_response(code: int, message: str = None, data: dict = None, req
return response
def exception_to_error_response(exception: Exception, code: int = None, request_id: str = None) -> dict:
def exception_to_error_response(exception: Exception, code: Optional[int] = None, request_id: Optional[str] = None) -> dict:
"""
将异常对象转换为统一格式的错误响应

View File

@@ -1,34 +0,0 @@
"""
JSON 工具模块
统一使用高性能的 orjson 库进行 JSON 序列化和反序列化。
如果 orjson 不可用,则回退到标准库 json。
"""
from typing import Any, Union
import json
# 在模块加载时检查 orjson 是否可用
try:
import orjson
_orjson_available = True
except ImportError:
_orjson_available = False
def dumps(obj: Any) -> str:
"""
将对象序列化为 JSON 字符串。
"""
if _orjson_available:
# orjson.dumps 返回 bytes需要 decode
return orjson.dumps(obj).decode("utf-8")
else:
return json.dumps(obj, ensure_ascii=False)
def loads(json_str: Union[str, bytes]) -> Any:
"""
将 JSON 字符串反序列化为对象。
"""
if _orjson_available:
return orjson.loads(json_str)
else:
return json.loads(json_str)

View File

@@ -109,7 +109,7 @@ class PerformanceStats:
performance_stats = PerformanceStats()
def timeit(func: Callable = None, *, log_level: int = logging.INFO, collect_stats: bool = True):
def timeit(func: Optional[Callable] = None, *, log_level: int = logging.INFO, collect_stats: bool = True):
"""
函数执行时间分析装饰器(支持同步和异步)
@@ -261,7 +261,7 @@ class memory_profile:
logger.info(f"[内存分析] 使用内存: {memory_used:.2f} MB")
def memory_profile_decorator(func: Callable = None, *, interval: float = 0.1):
def memory_profile_decorator(func: Optional[Callable] = None, *, interval: float = 0.1):
"""
内存分析装饰器(支持同步函数)
@@ -296,7 +296,7 @@ def memory_profile_decorator(func: Callable = None, *, interval: float = 0.1):
return decorator(func)
def performance_monitor(func: Callable = None, *, threshold: float = 1.0):
def performance_monitor(func: Optional[Callable] = None, *, threshold: float = 1.0):
"""
性能监控装饰器
仅当函数执行时间超过阈值时记录日志

View File

@@ -1,7 +1,7 @@
"""
通用单例模式基类
"""
from typing import Any, Dict, Optional, Type, TypeVar
from typing import Any, Dict, Optional, Type, TypeVar, cast
T = TypeVar('T')
@@ -29,9 +29,9 @@ class Singleton:
Returns:
T: 单例实例
"""
# 使用全局字典存储实例,避免类型检查问题
# 使用全局字典存储实例,修复类型检查问题
if cls not in _instance_store:
_instance_store[cls] = super().__new__(cls)
_instance_store[cls] = super(Singleton, cls).__new__(cls)
return _instance_store[cls]
def __init__(self) -> None:
@@ -67,7 +67,7 @@ def singleton(cls: Type[T]) -> Type[T]:
nonlocal class_instance
if class_instance is None:
# 使用super()调用原始类的__new__方法
class_instance = cls(*args, **kwargs)
class_instance = super(SingletonClass, cls).__new__(cls)
return class_instance
# 复制类的元数据