style: 统一代码风格并进行现代化改进
对整个代码库进行了一次全面的风格统一和现代化改进。主要变更包括:
- 将 `hasattr` 等内置函数中的字符串参数从单引号 `'` 统一为双引号 `"`。
- 采用现代类型注解,例如将 `Optional[T]` 替换为 `T | None`,`List[T]` 替换为 `list[T]` 等。
- 移除不再需要的 Python 2 兼容性声明 `# -*- coding: utf-8 -*-`。
- 清理了多余的空行、注释和未使用的导入。
- 统一了文件末尾的换行符。
- 优化了部分日志输出和字符串格式化 (`f"{e!s}"`)。
这些改动旨在提升代码的可读性、一致性和可维护性,使其更符合现代 Python 编码规范。
This commit is contained in:
@@ -5,9 +5,8 @@
|
||||
|
||||
import asyncio
|
||||
import time
|
||||
import weakref
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, Dict, Optional, Set
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
@@ -69,7 +68,7 @@ class ConnectionPoolManager:
|
||||
self.max_idle = max_idle
|
||||
|
||||
# 连接池
|
||||
self._connections: Set[ConnectionInfo] = set()
|
||||
self._connections: set[ConnectionInfo] = set()
|
||||
self._lock = asyncio.Lock()
|
||||
|
||||
# 统计信息
|
||||
@@ -83,7 +82,7 @@ class ConnectionPoolManager:
|
||||
}
|
||||
|
||||
# 后台清理任务
|
||||
self._cleanup_task: Optional[asyncio.Task] = None
|
||||
self._cleanup_task: asyncio.Task | None = None
|
||||
self._should_cleanup = False
|
||||
|
||||
logger.info(f"连接池管理器初始化完成 (最大池大小: {max_pool_size})")
|
||||
@@ -144,7 +143,7 @@ class ConnectionPoolManager:
|
||||
|
||||
yield connection_info.session
|
||||
|
||||
except Exception as e:
|
||||
except Exception:
|
||||
# 发生错误时回滚连接
|
||||
if connection_info and connection_info.session:
|
||||
try:
|
||||
@@ -157,7 +156,7 @@ class ConnectionPoolManager:
|
||||
if connection_info:
|
||||
connection_info.mark_released()
|
||||
|
||||
async def _get_reusable_connection(self, session_factory: async_sessionmaker[AsyncSession]) -> Optional[ConnectionInfo]:
|
||||
async def _get_reusable_connection(self, session_factory: async_sessionmaker[AsyncSession]) -> ConnectionInfo | None:
|
||||
"""获取可复用的连接"""
|
||||
async with self._lock:
|
||||
# 清理过期连接
|
||||
@@ -231,7 +230,7 @@ class ConnectionPoolManager:
|
||||
self._connections.clear()
|
||||
logger.info("所有连接已关闭")
|
||||
|
||||
def get_stats(self) -> Dict[str, Any]:
|
||||
def get_stats(self) -> dict[str, Any]:
|
||||
"""获取连接池统计信息"""
|
||||
return {
|
||||
**self._stats,
|
||||
@@ -244,7 +243,7 @@ class ConnectionPoolManager:
|
||||
|
||||
|
||||
# 全局连接池管理器实例
|
||||
_connection_pool_manager: Optional[ConnectionPoolManager] = None
|
||||
_connection_pool_manager: ConnectionPoolManager | None = None
|
||||
|
||||
|
||||
def get_connection_pool_manager() -> ConnectionPoolManager:
|
||||
@@ -266,4 +265,4 @@ async def stop_connection_pool():
|
||||
global _connection_pool_manager
|
||||
if _connection_pool_manager:
|
||||
await _connection_pool_manager.stop()
|
||||
_connection_pool_manager = None
|
||||
_connection_pool_manager = None
|
||||
|
||||
Reference in New Issue
Block a user