"""网关容器:集中管理网关侧服务实例,用于依赖注入""" from __future__ import annotations import asyncio import logging from dataclasses import dataclass from app.gateway.db import GatewayDB from app.gateway.query_queue import QueryQueue from app.gateway.task_queue import TaskQueue from app.shared.config import Settings logger = logging.getLogger(__name__) @dataclass(slots=True) class GatewayContainer: """网关容器 与抓取/交易容器同样的依赖注入风格,但持有的是任务队列与 SQLite 连接, 生命周期由 main.lifespan 管理:start 时打开 DB,close 时关闭。 两条通道各有一个队列对象,共用同一个 GatewayDB: - `task_queue` 下单任务(不可逆写,租约过期只置 stale) - `query_queue` 账号只读查询(可安全重投,见 query_queue.py 顶部对照表) `sweep_task` 是常驻后台扫描,把过期的 leased/running 推到 stale,并顺带扫 一轮查询单(重投 / 过期 / 清理过保留期的结果)。即使没有 lease 请求,过期的 任务也会被及时发现(规格 §5 的关键约束)。 """ settings: Settings db: GatewayDB task_queue: TaskQueue query_queue: QueryQueue sweep_task: asyncio.Task | None = None