1c9339a4db
- config/settings.py:Pydantic 解析 env.yaml - data/db.py:asyncpg 连接池管理 - data/reader.py:KlineReader 只读查询 TimescaleDB - data/models.py:KlineRecord 等 Pydantic 模型,镜像 TypeORM 实体 - example/test_db.py:数据库查询验证示例 - README.md:引擎架构文档
29 lines
762 B
Python
29 lines
762 B
Python
"""asyncpg 连接池管理 —— engine 模块对 TimescaleDB 的唯一切入点。"""
|
|
|
|
import asyncpg
|
|
from engine.config.settings import DB_DSN, db as db_config
|
|
|
|
_pool: asyncpg.Pool | None = None
|
|
|
|
|
|
async def get_pool() -> asyncpg.Pool:
|
|
"""获取或创建 asyncpg 连接池(懒初始化,复用)。"""
|
|
global _pool
|
|
if _pool is None:
|
|
_pool = await asyncpg.create_pool(
|
|
dsn=DB_DSN,
|
|
min_size=2,
|
|
max_size=10,
|
|
command_timeout=30,
|
|
server_settings={"default_transaction_read_only": "on"},
|
|
)
|
|
return _pool
|
|
|
|
|
|
async def close_pool() -> None:
|
|
"""关闭连接池(应用退出时调用)"""
|
|
global _pool
|
|
if _pool:
|
|
await _pool.close()
|
|
_pool = None
|