From e83e0d9ff2a2454ddaf2fe8941f5dad84bf41340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9B=85=E8=AF=BA=E7=8B=90?= <212194964+foxcyber907@users.noreply.github.com> Date: Mon, 6 Oct 2025 21:02:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E8=B0=83=E7=94=A8=E5=92=8C=E6=9D=83=E9=99=90=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除bot.py中不必要的asyncio.to_thread包装 - 将权限API的is_master方法改为异步调用 - 删除不再使用的SQLAlchemyTransaction类 --- bot.py | 2 +- src/common/database/database.py | 29 ------------------- src/plugin_system/apis/permission_api.py | 6 ++-- .../built_in/permission_management/plugin.py | 2 +- 4 files changed, 5 insertions(+), 34 deletions(-) diff --git a/bot.py b/bot.py index 93b22c307..96c5f165f 100644 --- a/bot.py +++ b/bot.py @@ -289,7 +289,7 @@ class DatabaseManager: start_time = time.time() # 使用线程执行器运行潜在的阻塞操作 - await asyncio.to_thread(initialize_sql_database, global_config.database) + await initialize_sql_database( global_config.database) elapsed_time = time.time() - start_time logger.info( f"数据库连接初始化成功,使用 {global_config.database.database_type} 数据库,耗时: {elapsed_time:.2f}秒" diff --git a/src/common/database/database.py b/src/common/database/database.py index 894ec5f2b..c6d9ec2c7 100644 --- a/src/common/database/database.py +++ b/src/common/database/database.py @@ -48,35 +48,6 @@ class DatabaseProxy: return result -class SQLAlchemyTransaction: - """SQLAlchemy 异步事务上下文管理器 (兼容旧代码示例,推荐直接使用 get_db_session)。""" - - def __init__(self): - self._ctx = None - self.session = None - - async def __aenter__(self): - # get_db_session 是一个 async contextmanager - self._ctx = get_db_session() - self.session = await self._ctx.__aenter__() - return self.session - - async def __aexit__(self, exc_type, exc_val, exc_tb): - try: - if self.session: - if exc_type is None: - try: - await self.session.commit() - except Exception: - await self.session.rollback() - raise - else: - await self.session.rollback() - finally: - if self._ctx: - await self._ctx.__aexit__(exc_type, exc_val, exc_tb) - - # 创建全局数据库代理实例 db = DatabaseProxy() diff --git a/src/plugin_system/apis/permission_api.py b/src/plugin_system/apis/permission_api.py index 3c42f9eab..41b6a761b 100644 --- a/src/plugin_system/apis/permission_api.py +++ b/src/plugin_system/apis/permission_api.py @@ -36,7 +36,7 @@ class IPermissionManager(ABC): async def check_permission(self, user: UserInfo, permission_node: str) -> bool: ... @abstractmethod - def is_master(self, user: UserInfo) -> bool: ... # 同步快速判断 + async def is_master(self, user: UserInfo) -> bool: ... # 同步快速判断 @abstractmethod async def register_permission_node(self, node: PermissionNode) -> bool: ... @@ -82,9 +82,9 @@ class PermissionAPI: self._ensure_manager() return await self._permission_manager.check_permission(UserInfo(platform, user_id), permission_node) - def is_master(self, platform: str, user_id: str) -> bool: + async def is_master(self, platform: str, user_id: str) -> bool: self._ensure_manager() - return self._permission_manager.is_master(UserInfo(platform, user_id)) + return await self._permission_manager.is_master(UserInfo(platform, user_id)) async def register_permission_node( self, diff --git a/src/plugins/built_in/permission_management/plugin.py b/src/plugins/built_in/permission_management/plugin.py index 1541e2f1c..be9ffad7c 100644 --- a/src/plugins/built_in/permission_management/plugin.py +++ b/src/plugins/built_in/permission_management/plugin.py @@ -231,7 +231,7 @@ class PermissionCommand(PlusCommand): target_user_id = chat_stream.user_info.user_id # 检查是否为Master用户 - is_master = permission_api.is_master(chat_stream.platform, target_user_id) + is_master = await permission_api.is_master(chat_stream.platform, target_user_id) # 获取用户权限 permissions = await permission_api.get_user_permissions(chat_stream.platform, target_user_id)