feat: 添加 WithDefaultTimeout 默认超时配置

Invoke 之前完全依赖调用方传入的 ctx 控制超时,池子被占满或 Python 侧
handler 阻塞时,未设置 deadline 的调用会永久阻塞且不报错。新增
WithDefaultTimeout 选项,仅在 ctx 未设置 deadline 时兜底生效,调用方
显式设置的超时优先级更高。

同时补充 example 中的阻塞/超时演示(demoTimeout、demoBlocking)和
pool_test.go 集成测试,覆盖池占满排队、默认超时、显式 deadline 优先级、
流式输出超时后 channel 静默关闭等场景。
This commit is contained in:
2026-07-23 10:37:24 +08:00
parent db71a904e0
commit 0f4a4ded53
7 changed files with 367 additions and 7 deletions
+19
View File
@@ -5,6 +5,7 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "python"))
import dataclasses
import threading
import time
from typing import Iterator
from gobridge import expose, call_go, run, worker_id, worker_count
@@ -120,6 +121,24 @@ def bytes_chunks(data: bytes, size: int):
yield data[i:i + size]
# ── 超时示例 ─────────────────────────────────────────────────────────────────
@expose
def sleep_seconds(n: float) -> str:
"""模拟一次耗时阻塞调用(不检查 ctx 取消),用于演示 WithDefaultTimeout"""
time.sleep(n)
return f"slept {n}s"
@expose
def slow_range_gen(start: int, stop: int, delay_ms: int) -> Iterator[int]:
"""流式输出,每个元素之间人为延迟,用于演示 ctx 超时会提前关闭 channel"""
for i in range(start, stop):
time.sleep(delay_ms / 1000)
yield i
# ── Server 全双工示例 ────────────────────────────────────────────────────────