6 Commits
Author SHA1 Message Date
openbot 0618062e54 chore: bump version 0.6.3 2026-10-18 16:12:51 +08:00
openbot 9c97fea71b fix(pool): ExPool acquire backpressure + ex_pool_cap (fixes #1)
- acquire(): replace 3x0.05s fast-fail with Condition-based queueing
  (default acquire_timeout=30s); timeout error carries pool state
  (idle/total/servers/per_server_cap)
- api: expose ex_pool_cap (default 2, aligned with std/mac pool caps);
  single-server scenario no longer silently capped at 1 concurrent
2026-10-18 16:10:15 +08:00
openbot-coder 2d62d82b51 feat: 新增3台EX扩展行情服务器(connect.cfg官方扩展市场,handshake=login),全量复测30/36可用 2026-10-16 13:53:19 +08:00
openbot-coder 7e72f70eb2 chore: 离线探针复测写回 servers.json(2026-10-16,可达25→27,117.34.114.13/.14 复活) 2026-10-16 12:51:12 +08:00
openbotandopenbot-coder b2f899df49 fix(models): 修复美股分钟K线 datetime 时区拼接错位,还原为北京时间 (#2)
通达信 EX 通道对美股返回的分钟级 K线把两个时区拼进同一个五元组:日期分量是
美东交易日(zipday),时分分量是北京时间(tminutes)。直拼出的 datetime 会把
同一交易日的下午半场排到上午半场之前——单日 390 根首末颠倒,且全部落在同一个
北京自然日里,导致排序 / count 切片 / 日期区间二分与过滤 / 增量水位线全部不可用。

在 SecurityBar.__post_init__ 统一归一化为北京时间(凌晨段日期 +1 天,含月末与
年末进位),EX 的两条 K线命令路径(_bars_ex 的 0x23FF 与二分所依赖的 MAC 0x122E)
因此共用同一口径,不会出现"取数按一套时间、二分按另一套时间"的错位。

新增两个只读属性:trading_date(归属交易日;美股一个交易日横跨两个北京自然日,
按交易日聚合必须用它,不能用 datetime.date())与 is_us。

实测(安装 wheel 0.6.2,非源码树):10-06 ~ 10-12 五个完整交易日各 390 根、
严格单调,首根 open / 末根 close 与日线逐位相同,成交量差 <= 1;A股 / 港股零回归。

- 版本 0.6.1 -> 0.6.2(pyproject / __init__ / uv.lock)
- tests/test_protocol.py 新增 9 项回归测试(分界点 / 月末年末进位 / 390 根整日单调性 / MAC 路径),全量 191 passed
- CHANGELOG / README / SKILL.md / docs(faq、llms.txt、api/models)同步口径说明

遗留(本版未含,待决策):EX 分时对美股返回的 MinuteBar.hour 是未取模的连续分钟数
(实测可达 27,即次日 03:xx),且 MinuteBar 无日期分量可承载跨日信息。

Co-authored-by: openbot-coder <openbot-coder@users.noreply.github.com>
2026-10-13 16:47:57 +08:00
openbot-coder 8193d560da Revert "fix(models): 修复美股分钟K线 datetime 时区拼接错位,还原为北京时间"
This reverts commit 5408903a4c.
2026-10-13 16:42:41 +08:00
6 changed files with 405 additions and 173 deletions

No files matched your search

+5
View File
@@ -2,6 +2,11 @@
本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。 本项目遵循 [语义化版本](https://semver.org/lang/zh-CN/)。
## [0.6.3] - 2026-10-18
- fix(pool): ExPool acquire 背压等待(Condition 排队,默认 30s 超时)替代 3x0.05s 快速失败;超时异常带池状态
- feat(api): 新增 ex_pool_cap 参数(默认 2),单 EX 服务器场景不再静默退化为 1 并发(fixes #1)
## [0.6.2] - 2026-10-13 ## [0.6.2] - 2026-10-13
### 修复 ### 修复
+1 -1
View File
@@ -1,6 +1,6 @@
[project] [project]
name = "pytdxdata" name = "pytdxdata"
version = "0.6.2" version = "0.6.3"
description = "通达信(TongDaXin)行情数据客户端:动态连接池 + 双层TTL缓存 + 3连接并发分页,纯asyncio零运行时依赖,支持A股/港股/美股/期货/期权" description = "通达信(TongDaXin)行情数据客户端:动态连接池 + 双层TTL缓存 + 3连接并发分页,纯asyncio零运行时依赖,支持A股/港股/美股/期货/期权"
readme = "README.md" readme = "README.md"
requires-python = ">=3.11" requires-python = ">=3.11"
+5
View File
@@ -233,11 +233,13 @@ class TdxData:
pool_max: int = 12, pool_max: int = 12,
mac_pool_cap: int = 4, mac_pool_cap: int = 4,
std_pool_cap: int = 4, std_pool_cap: int = 4,
ex_pool_cap: int = 2,
) -> None: ) -> None:
self.port = port self.port = port
self.timeout = timeout self.timeout = timeout
self.default_adjust = default_adjust self.default_adjust = default_adjust
self.mac_pool_cap = mac_pool_cap self.mac_pool_cap = mac_pool_cap
self.ex_pool_cap = ex_pool_cap
self._std_servers = list(standard_servers or get_standard_hosts()) self._std_servers = list(standard_servers or get_standard_hosts())
self._mac_servers = list(mac_servers or get_mac_hosts()) self._mac_servers = list(mac_servers or get_mac_hosts())
self._ex_servers = list(ex_servers or get_ex_hosts()) self._ex_servers = list(ex_servers or get_ex_hosts())
@@ -262,8 +264,11 @@ class TdxData:
probe_factory=lambda: MacKlineCmd(1, "600000", KlinePeriod.DAY, Adjust.NONE, 0, 100), probe_factory=lambda: MacKlineCmd(1, "600000", KlinePeriod.DAY, Adjust.NONE, 0, 100),
) )
# EX扩展市场池(港股/期货/美股,端口7727;MAC EX login + pytdx EX setup 混合) # EX扩展市场池(港股/期货/美股,端口7727;MAC EX login + pytdx EX setup 混合)
# 单服务器场景(测速缓存收缩后仅 1 台)下,有效并发 = 服务器数 × ex_pool_cap,
# 默认 cap=2 保证至少 2 并发;acquire 有 Condition 背压等待,不再快速失败。
self._ex_pool = ExPool(self._ex_servers, timeout=timeout, self._ex_pool = ExPool(self._ex_servers, timeout=timeout,
min_size=min(pool_min, 2), max_size=min(pool_max, 6), min_size=min(pool_min, 2), max_size=min(pool_max, 6),
per_server_cap=ex_pool_cap,
handshake_map=EX_HANDSHAKES) handshake_map=EX_HANDSHAKES)
disk = None disk = None
if self._cache_dir is not None: if self._cache_dir is not None:
+81 -32
View File
@@ -1,4 +1,11 @@
"""EX扩展市场连接池(简化版:固定连接数,借还+坏连接重建+login自动)""" """EX扩展市场连接池(固定连接数,借还+坏连接重建+login自动)。
与标准池 pool.py 的背压语义对齐:acquire 在无空闲连接且无法扩容时,
基于 asyncio.Condition 排队等待 release 唤醒(默认 acquire_timeout=30s),
而不是快速失败。per_server_cap 限制单台服务器连接数;单服务器场景下
有效并发 = servers × per_server_cap(默认 1),通过 TdxData(ex_pool_cap=N)
可对同一 EX 主机开多连接。
"""
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
@@ -9,6 +16,23 @@ from .ex_connection import ExTdxConnection
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
DEFAULT_ACQUIRE_TIMEOUT = 30.0
class ExPoolTimeoutError(ConnectionError):
"""acquire 等待超时(携带池状态快照,便于排障)。"""
def __init__(self, pool: "ExPool", timeout: float) -> None:
idle = len(pool._idle)
total = pool._total
servers = len(pool.servers)
cap = pool.per_server_cap
super().__init__(
f"EX连接池获取超时(等了 {timeout:.1f}s):"
f"idle={idle} total={total} servers={servers} per_server_cap={cap} "
f"有效并发={servers * cap}"
)
class ExPool: class ExPool:
"""EX通道连接池(港股/期货,端口7727,连接时自动login)。""" """EX通道连接池(港股/期货,端口7727,连接时自动login)。"""
@@ -16,6 +40,7 @@ class ExPool:
def __init__(self, servers: list[str], *, timeout: float = 10.0, def __init__(self, servers: list[str], *, timeout: float = 10.0,
min_size: int = 2, max_size: int = 6, min_size: int = 2, max_size: int = 6,
per_server_cap: int = 1, per_server_cap: int = 1,
acquire_timeout: float | None = None,
handshake: str = "login", handshake: str = "login",
handshake_map: dict | None = None) -> None: handshake_map: dict | None = None) -> None:
self.servers = servers self.servers = servers
@@ -25,14 +50,17 @@ class ExPool:
self.min_size = min_size self.min_size = min_size
self.max_size = max_size self.max_size = max_size
self.per_server_cap = max(1, per_server_cap) # 一台服务器最多几个连接(默认1=单连接) self.per_server_cap = max(1, per_server_cap) # 一台服务器最多几个连接(默认1=单连接)
self.acquire_timeout = (
DEFAULT_ACQUIRE_TIMEOUT if acquire_timeout is None else acquire_timeout
)
self._idle: list[tuple[str, ExTdxConnection]] = [] self._idle: list[tuple[str, ExTdxConnection]] = []
self._server_count: dict[str, int] = {} self._server_count: dict[str, int] = {}
self._total = 0 self._total = 0
self._closed = False self._closed = False
self._lock = asyncio.Lock() self._cond = asyncio.Condition()
async def start(self) -> None: async def start(self) -> None:
async with self._lock: async with self._cond:
for host in self.servers[: self.min_size]: for host in self.servers[: self.min_size]:
if self._server_count.get(host, 0) >= self.per_server_cap: if self._server_count.get(host, 0) >= self.per_server_cap:
continue continue
@@ -47,7 +75,7 @@ class ExPool:
async def close(self) -> None: async def close(self) -> None:
self._closed = True self._closed = True
async with self._lock: async with self._cond:
for _, conn in self._idle: for _, conn in self._idle:
try: try:
await conn.close() await conn.close()
@@ -56,48 +84,61 @@ class ExPool:
self._idle.clear() self._idle.clear()
self._total = 0 self._total = 0
self._server_count.clear() self._server_count.clear()
self._cond.notify_all()
async def acquire(self) -> ExTdxConnection: async def acquire(self, timeout: float | None = None) -> ExTdxConnection:
for _ in range(3): """借出一条连接;无空闲且无法扩容时排队等待 release 唤醒。
stale_conn = None
new_conn = None timeout 默认取 self.acquire_timeout(30s);传 None 表示无限等待。
new_host = None """
async with self._lock: if timeout is None:
timeout = self.acquire_timeout
deadline = None if timeout is None else time.monotonic() + timeout
async with self._cond:
while True:
# 1. 优先借用空闲连接(过期则丢弃,槽位在锁内回收)
if self._idle: if self._idle:
_, conn = self._idle.pop() _, conn = self._idle.pop()
fresh = time.monotonic() - conn.last_used < 30.0 fresh = time.monotonic() - conn.last_used < 30.0
if conn.alive and fresh: if conn.alive and fresh:
return conn return conn
# 连接空闲过久(login会话可能失效)→ 丢弃重连 # 连接空闲过久(login会话可能失效)→ 丢弃,槽位让给扩容
self._total = max(0, self._total - 1) self._total = max(0, self._total - 1)
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1) self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
stale_conn = conn self._schedule_close(conn)
# 扩容:在锁内预留槽位,锁外执行网络IO
elif self._total < self.max_size and self.servers: # 2. 可扩容 → 锁内预留槽位,锁外建连
new_conn = None
new_host = None
if self._total < self.max_size and self.servers:
host = self._next_available_server() host = self._next_available_server()
if host: if host is not None:
self._total += 1 self._total += 1
self._server_count[host] = self._server_count.get(host, 0) + 1 self._server_count[host] = self._server_count.get(host, 0) + 1
new_conn = ExTdxConnection(host, 7727, self.timeout, self.handshake_map.get(host, self.handshake)) new_conn = ExTdxConnection(host, 7727, self.timeout, self.handshake_map.get(host, self.handshake))
new_host = host new_host = host
# 锁外:关闭过期连接、建立新连接
if stale_conn is not None:
try:
await stale_conn.close()
except Exception:
pass
if new_conn is not None: if new_conn is not None:
# 锁外网络 IO:连接失败回滚槽位后继续等待
try: try:
await new_conn.connect() await new_conn.connect()
return new_conn return new_conn
except Exception as e: except Exception as e:
log.warning("EX扩容失败 %s: %s", new_host, str(e)[:60]) log.warning("EX扩容失败 %s: %s", new_host, str(e)[:60])
# 连接失败,回滚预留槽位 async with self._cond:
async with self._lock:
self._total = max(0, self._total - 1) self._total = max(0, self._total - 1)
self._server_count[new_host] = max(0, self._server_count.get(new_host, 0) - 1) self._server_count[new_host] = max(0, self._server_count.get(new_host, 0) - 1)
await asyncio.sleep(0.05)
raise ConnectionError("EX连接池无可用连接") # 3. 池满:排队等待 release 唤醒(或超时)
now = time.monotonic()
if deadline is not None and now >= deadline:
raise ExPoolTimeoutError(self, timeout)
try:
if deadline is None:
await self._cond.wait()
else:
await asyncio.wait_for(self._cond.wait(), timeout=deadline - now)
except asyncio.TimeoutError:
raise ExPoolTimeoutError(self, timeout)
def _next_available_server(self) -> str | None: def _next_available_server(self) -> str | None:
for h in self.servers: for h in self.servers:
@@ -105,15 +146,23 @@ class ExPool:
return h return h
return None return None
async def release(self, conn: ExTdxConnection, *, broken: bool = False) -> None: def _schedule_close(self, conn: ExTdxConnection) -> None:
if broken or not conn.alive: async def _close():
async with self._lock:
self._total = max(0, self._total - 1)
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
try: try:
await conn.close() await conn.close()
except Exception: except Exception:
pass pass
return try:
async with self._lock: asyncio.get_running_loop().create_task(_close())
except RuntimeError:
pass
async def release(self, conn: ExTdxConnection, *, broken: bool = False) -> None:
async with self._cond:
if broken or not conn.alive:
self._total = max(0, self._total - 1)
self._server_count[conn.host] = max(0, self._server_count.get(conn.host, 0) - 1)
self._schedule_close(conn)
else:
self._idle.append((conn.host, conn)) self._idle.append((conn.host, conn))
self._cond.notify_all()
+197 -140
View File
@@ -1,6 +1,6 @@
{ {
"version": "2026-09-21", "version": "2026-10-16",
"note": "离线探针复测(2026-09-21 19:03): 可达=25/33, 入池fast_pool=10台", "note": "离线探针复测(2026-10-16 13:46): 可达=30/36, 入池fast_pool=10台",
"servers": [ "servers": [
{ {
"ip": "139.9.52.158", "ip": "139.9.52.158",
@@ -11,14 +11,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.22, "latency": 0.172,
"checked_at": "2026-09-21 19:03:04", "checked_at": "2026-10-16 13:46:17",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": true, "fast_pool": true,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.226, "standard": 0.172,
"mac": 0.22 "mac": 0.183
} }
}, },
{ {
@@ -30,14 +30,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.366, "latency": 0.298,
"checked_at": "2026-09-21 19:03:04", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": true,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.396, "standard": 0.316,
"mac": 0.366 "mac": 0.298
} }
}, },
{ {
@@ -48,13 +48,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.257, "latency": 0.236,
"checked_at": "2026-09-21 19:03:09", "checked_at": "2026-10-16 13:46:23",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": true, "fast_pool": true,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.257 "mac": 0.236
} }
}, },
{ {
@@ -65,13 +65,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.248, "latency": 0.213,
"checked_at": "2026-09-21 19:03:04", "checked_at": "2026-10-16 13:46:17",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": true, "fast_pool": true,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.248 "mac": 0.213
} }
}, },
{ {
@@ -84,15 +84,15 @@
], ],
"handshake": "setup", "handshake": "setup",
"status": "ok", "status": "ok",
"latency": 0.221, "latency": 0.316,
"checked_at": "2026-09-21 19:03:00", "checked_at": "2026-10-16 13:46:13",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": true, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.399, "standard": 0.316,
"mac": 0.414, "mac": 0.329,
"ex": 0.221 "ex": 0.539
} }
}, },
{ {
@@ -104,10 +104,10 @@
"handshake": null, "handshake": null,
"status": "fail", "status": "fail",
"latency": null, "latency": null,
"checked_at": "2026-09-21 19:03:20", "checked_at": "2026-10-16 13:46:34",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": {} "_per_proto": {}
}, },
{ {
@@ -118,13 +118,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.363, "latency": 0.682,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:24",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": true, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.363 "mac": 0.682
} }
}, },
{ {
@@ -136,10 +136,10 @@
"handshake": null, "handshake": null,
"status": "fail", "status": "fail",
"latency": null, "latency": null,
"checked_at": "2026-09-21 19:03:20", "checked_at": "2026-10-16 13:46:34",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": {} "_per_proto": {}
}, },
{ {
@@ -150,13 +150,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.344, "latency": 0.607,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:23",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": true, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.344 "mac": 0.607
} }
}, },
{ {
@@ -167,13 +167,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.404, "latency": 1.264,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:24",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.404 "mac": 1.264
} }
}, },
{ {
@@ -185,10 +185,10 @@
"handshake": null, "handshake": null,
"status": "fail", "status": "fail",
"latency": null, "latency": null,
"checked_at": "2026-09-21 19:03:24", "checked_at": "2026-10-16 13:46:34",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": {} "_per_proto": {}
}, },
{ {
@@ -200,10 +200,10 @@
"handshake": null, "handshake": null,
"status": "fail", "status": "fail",
"latency": null, "latency": null,
"checked_at": "2026-09-21 19:03:21", "checked_at": "2026-10-16 13:46:34",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": {} "_per_proto": {}
}, },
{ {
@@ -214,13 +214,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.376, "latency": 0.612,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:23",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.376 "mac": 0.612
} }
}, },
{ {
@@ -231,13 +231,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.377, "latency": 0.613,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:23",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.377 "mac": 0.613
} }
}, },
{ {
@@ -248,13 +248,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.401, "latency": 0.624,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:23",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.401 "mac": 0.624
} }
}, },
{ {
@@ -265,13 +265,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.42, "latency": 0.592,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:23",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.42 "mac": 0.592
} }
}, },
{ {
@@ -282,13 +282,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.401, "latency": 0.624,
"checked_at": "2026-09-21 19:03:10", "checked_at": "2026-10-16 13:46:23",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"mac": 0.401 "mac": 0.624
} }
}, },
{ {
@@ -300,14 +300,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.485, "latency": 0.306,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.485, "standard": 0.352,
"mac": 0.605 "mac": 0.306
} }
}, },
{ {
@@ -319,10 +319,10 @@
"handshake": null, "handshake": null,
"status": "fail", "status": "fail",
"latency": null, "latency": null,
"checked_at": "2026-09-21 19:03:04", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": {} "_per_proto": {}
}, },
{ {
@@ -334,14 +334,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.444, "latency": 0.307,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.444, "standard": 0.35,
"mac": 0.903 "mac": 0.307
} }
}, },
{ {
@@ -352,13 +352,16 @@
"mac" "mac"
], ],
"handshake": null, "handshake": null,
"status": "fail", "status": "ok",
"latency": null, "latency": 0.395,
"checked_at": "2026-09-21 19:03:15", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": {} "_per_proto": {
"standard": 0.395,
"mac": 0.675
}
}, },
{ {
"ip": "117.34.114.15", "ip": "117.34.114.15",
@@ -369,14 +372,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.435, "latency": 0.345,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.463, "standard": 0.345,
"mac": 0.435 "mac": 0.726
} }
}, },
{ {
@@ -387,13 +390,16 @@
"mac" "mac"
], ],
"handshake": null, "handshake": null,
"status": "fail", "status": "ok",
"latency": null, "latency": 0.326,
"checked_at": "2026-09-21 19:03:14", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": {} "_per_proto": {
"standard": 0.326,
"mac": 0.743
}
}, },
{ {
"ip": "117.34.114.27", "ip": "117.34.114.27",
@@ -404,14 +410,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.482, "latency": 0.394,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.482, "standard": 0.394,
"mac": 0.579 "mac": 0.78
} }
}, },
{ {
@@ -423,14 +429,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.609, "latency": 0.327,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.946, "standard": 0.327,
"mac": 0.609 "mac": 0.328
} }
}, },
{ {
@@ -442,14 +448,14 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.46, "latency": 0.348,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"fast_pool": false, "fast_pool": false,
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"_per_proto": { "_per_proto": {
"standard": 0.46, "standard": 0.348,
"mac": 0.521 "mac": 0.74
} }
}, },
{ {
@@ -461,9 +467,9 @@
"handshake": null, "handshake": null,
"status": "fail", "status": "fail",
"latency": null, "latency": null,
"checked_at": "2026-09-21 19:03:24", "checked_at": "2026-10-16 13:46:37",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"fast_pool": false, "fast_pool": false,
"_per_proto": {} "_per_proto": {}
}, },
@@ -475,13 +481,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.271, "latency": 0.726,
"checked_at": "2026-09-21 19:03:04", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"fast_pool": true, "fast_pool": false,
"_per_proto": { "_per_proto": {
"mac": 0.271 "mac": 0.726
} }
}, },
{ {
@@ -492,13 +498,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.375, "latency": 0.624,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:18",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"fast_pool": false, "fast_pool": false,
"_per_proto": { "_per_proto": {
"mac": 0.375 "mac": 0.624
} }
}, },
{ {
@@ -509,13 +515,13 @@
], ],
"handshake": null, "handshake": null,
"status": "ok", "status": "ok",
"latency": 0.424, "latency": 0.56,
"checked_at": "2026-09-21 19:03:05", "checked_at": "2026-10-16 13:46:30",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"fast_pool": false, "fast_pool": false,
"_per_proto": { "_per_proto": {
"mac": 0.424 "mac": 0.56
} }
}, },
{ {
@@ -526,13 +532,13 @@
], ],
"handshake": "login", "handshake": "login",
"status": "ok", "status": "ok",
"latency": 0.176, "latency": 0.097,
"checked_at": "2026-09-21 19:03:08", "checked_at": "2026-10-16 13:46:21",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"fast_pool": true, "fast_pool": true,
"_per_proto": { "_per_proto": {
"ex": 0.176 "ex": 0.097
} }
}, },
{ {
@@ -543,13 +549,13 @@
], ],
"handshake": "login", "handshake": "login",
"status": "ok", "status": "ok",
"latency": 0.148, "latency": 0.097,
"checked_at": "2026-09-21 19:03:08", "checked_at": "2026-10-16 13:46:21",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"fast_pool": true, "fast_pool": true,
"_per_proto": { "_per_proto": {
"ex": 0.148 "ex": 0.097
} }
}, },
{ {
@@ -560,15 +566,66 @@
], ],
"handshake": "setup", "handshake": "setup",
"status": "ok", "status": "ok",
"latency": 0.166, "latency": 0.103,
"checked_at": "2026-09-21 19:03:08", "checked_at": "2026-10-16 13:46:21",
"source": "probe-2026-08-12", "source": "probe-2026-08-12",
"checked_date": "2026-09-21", "checked_date": "2026-10-16",
"fast_pool": true, "fast_pool": true,
"_per_proto": { "_per_proto": {
"ex": 0.166 "ex": 0.103
}
},
{
"ip": "112.74.214.43",
"name": "扩展市场深圳双线1",
"protocols": [
"ex"
],
"handshake": "login",
"status": "ok",
"latency": 0.095,
"checked_at": "2026-10-16 13:46:21",
"source": "connect.cfg",
"fast_pool": true,
"checked_date": "2026-10-16",
"_per_proto": {
"ex": 0.095
}
},
{
"ip": "120.25.218.6",
"name": "扩展市场深圳双线2",
"protocols": [
"ex"
],
"handshake": "login",
"status": "ok",
"latency": 0.101,
"checked_at": "2026-10-16 13:46:21",
"source": "connect.cfg",
"fast_pool": true,
"checked_date": "2026-10-16",
"_per_proto": {
"ex": 0.101
}
},
{
"ip": "47.102.108.214",
"name": "扩展市场上海双线2",
"protocols": [
"ex"
],
"handshake": "login",
"status": "ok",
"latency": 0.149,
"checked_at": "2026-10-16 13:46:22",
"source": "connect.cfg",
"fast_pool": true,
"checked_date": "2026-10-16",
"_per_proto": {
"ex": 0.149
} }
} }
], ],
"checked_date": "2026-09-21" "checked_date": "2026-10-16"
} }
+116
View File
@@ -0,0 +1,116 @@
"""ExPool 背压/并发语义回归(Issue #1:并发超限 0.15s 秒失败)。
用 mock 连接模拟:单服务器 + per_server_cap=1 时,
- 并发 acquire 超过有效并发 → 排队等待而不是快速失败;
- release 唤醒等待者;
- acquire 超时异常带池状态;
- ex_pool_cap 提升同主机并发。
"""
from __future__ import annotations
import asyncio
import time
import pytest
from pytdxdata.pool.ex_pool import ExPool, ExPoolTimeoutError
class MockConn:
"""Minimal stand-in for ExTdxConnection."""
last_created = 0
def __init__(self, host: str, port: int, timeout: float, handshake: str = "login"):
self.host = host
self.port = port
self.alive = True
self.last_used = time.monotonic()
self.closed = False
MockConn.last_created += 1
async def connect(self) -> None:
await asyncio.sleep(0) # instant "connect"
async def close(self) -> None:
self.closed = True
self.alive = False
@pytest.fixture
def patch_conn(monkeypatch):
monkeypatch.setattr("pytdxdata.pool.ex_pool.ExTdxConnection", MockConn)
MockConn.last_created = 0
async def test_acquire_queues_instead_of_fast_fail(patch_conn):
"""并发 > 有效并发时,后续 acquire 等待 release,而非 0.15s 抛错。"""
pool = ExPool(["e1"], min_size=1, max_size=6, per_server_cap=1)
await pool.start()
c1 = await pool.acquire() # 唯一连接被占用
t0 = time.monotonic()
async def holder():
await asyncio.sleep(0.5) # 持有 0.5s 后归还
await pool.release(c1)
asyncio.create_task(holder())
c2 = await pool.acquire() # 应在 ~0.5s 后拿到,而不是 0.15s 抛错
waited = time.monotonic() - t0
assert waited >= 0.4, f"acquire 没有等待释放(仅 {waited:.2f}s)"
assert c2 is c1 # 归还的是同一条连接
async def test_acquire_timeout_carries_pool_state(patch_conn):
"""超时异常信息带池状态(idle/total/servers/cap)。"""
pool = ExPool(["e1"], min_size=1, max_size=6, per_server_cap=1, acquire_timeout=0.3)
await pool.start()
c1 = await pool.acquire()
with pytest.raises(ExPoolTimeoutError) as ei:
await pool.acquire()
msg = str(ei.value)
assert "total=1" in msg and "per_server_cap=1" in msg and "servers=1" in msg
await pool.release(c1)
async def test_ex_pool_cap_allows_parallel_conns_same_host(patch_conn):
"""per_server_cap=2 时单主机可并发持有 2 条连接。"""
pool = ExPool(["e1"], min_size=1, max_size=6, per_server_cap=2)
await pool.start()
got = await asyncio.gather(pool.acquire(), pool.acquire())
assert len(got) == 2 and got[0] is not got[1]
assert MockConn.last_created == 2
for c in got:
await pool.release(c)
async def test_concurrent_acquire_all_succeed(patch_conn):
"""Issue 场景:单服务器 + cap=2,6 个并发任务应全部拿到连接(排队)而非部分失败。"""
pool = ExPool(["e1"], min_size=2, max_size=6, per_server_cap=2)
await pool.start()
async def task(i):
c = await pool.acquire()
await asyncio.sleep(0.1) # 模拟一次 1m 分页拉取
await pool.release(c)
return i
results = await asyncio.gather(*[task(i) for i in range(6)])
assert sorted(results) == list(range(6)) # 全部成功,零失败
async def test_release_notifies_waiters(patch_conn):
"""broken 归还(连接死亡)也要唤醒等待者(槽位回收后可扩容)。"""
pool = ExPool(["e1"], min_size=1, max_size=1, per_server_cap=1)
await pool.start()
c1 = await pool.acquire()
c1.alive = False
async def broken_release():
await asyncio.sleep(0.2)
await pool.release(c1, broken=True)
asyncio.create_task(broken_release())
c2 = await pool.acquire() # 槽位被回收→扩容新建
assert c2 is not c1
await pool.release(c2)