Skip to content

fix(gateway): harden request pacing and fail fast on a bad listen address - #118

Open
Meepo268 wants to merge 2 commits into
jasonxu114514:mainfrom
Meepo268:pr/gateway-hardening
Open

Meepo268 wants to merge 2 commits into
jasonxu114514:mainfrom
Meepo268:pr/gateway-hardening

Conversation

@Meepo268

@Meepo268 Meepo268 commented Oct 17, 2026 •

Copy link
Copy Markdown

摘要

两处硬化:启动参数写错时别再"优雅退出 0",以及几处会把 key/出口错误判死的边界。

启动就先检查地址。 listen(和启用时的 webui.listen)写错,以前只在 ListenAndServe 里炸,而那个 goroutine 炸完会打一行 server stopped unexpectedly、取消 context、优雅退出,退出码 0。systemd / Docker / Kubernetes 都按退出码决定要不要重启,于是"地址写错"变成了"服务静默不干活且没人拉起"。现在在任何监听创建之前先用 net.ResolveTCPAddr 校验,失败打印 invalid_listen_address 并以 1 退出。同时给 API 和 WebUI server 加 ReadTimeout: 60s,请求体只发一半的连接不会无限占着;WriteTimeout 故意不设,因为 SSE 本来就要持续几分钟。

运行时的三个尖角。 上游在首个 SSE 事件之前灌超过 1 MiB 的横幅字节(注释/心跳)不再判失败、也不再冷却这个 key/代理,缓冲的字节会重放给客户端;没有上游 key、也没开匿名通道时 /health 不再报 ready: true;上游给的离谱 Retry-After 不能再把 key 挂几个小时,Key 档现在也按匿名档一直在用的 8× 上限钳制。

主要改动

启动与 server(cmd/opencode2api/main.go)

  • runtime 构造完之后、创建监听之前,用 net.ResolveTCPAddr("tcp", …) 校验两个地址;失败分别打 invalid_listen_address / invalid webui listen address(带 address、error)并 os.Exit(1)。
  • webui.listen 只在 webui.enabled 为真时校验,关掉的块里留着旧地址不会挡住启动。
  • API 与 WebUI 都加 ReadTimeout: 60 * time.Second(配合已有的 ReadHeaderTimeout: 15s、IdleTimeout: 120s);WriteTimeout 保持不设。

SSE 首事件(internal/gateway/first_event.go)

  • readFirstEvent 超限时不再丢掉已读到的前缀,而是连前缀一起返回新的哨兵错误 errPreludeTooLarge。
  • doInferenceAttempt 把它当成"别再等首事件,但这次尝试继续":仍然尊重尝试截止时间(commitDeadline),否则用一层包装把缓冲前缀接在实时读取器前面,和正常首事件路径一致。

就绪判定(internal/gateway/health.go)

  • blocking(进而 ready 和 HTTP 状态码)在 zenKeys+goKeys == 0 && !cfg.Anonymous 时也置位,和已有的 no_upstream_keys 条目对齐。

冷却上限(internal/gateway/pool.go)

  • 新增 maxKeyRetryAfterMultiplier = 8;nodePool.MarkFailure 把解析出的 Retry-After 钳到 min(retryAfter, p.cooldown*8)。匿名档原有的 8× 上限不变,相关注释改成不再声称 Key 档会完整遵守 Retry-After。

尝试编号(internal/gateway/upstream.go)

  • doUpstream 变成 doUpstreamOffset(ctx, route, bodies, ids, attemptOffset int) 的薄包装;悬空引用重放从 attemptOffset+attempts 重新开始,调用方自带偏移时编号也保持单调。

评审注意

  • 启动时会被拒绝的形式:8080(只有端口)、localhost(只有主机)、http://localhost:8080(带 scheme)、::1:8080(IPv6 未加方括号)、127.0.0.1:99999(端口越界)。接受 :8080、127.0.0.1:8080、localhost:8080、[::1]:8080、:0。错误文案直接来自 net,例如 address 8080: missing port in address。
  • 退出码变了,并已在基线上对比验证:基线打 server listening → server stopped unexpectedly 后退出 0;本分支打一行 invalid_listen_address 后退出 1。
  • os.Exit(1) 会跳过 defer manager.Shutdown()。这个时点没有需要 flush 的东西(日志已同步写出),且要的就是立刻退出,而不是再演一次"优雅退出 0"。
  • /health 的变化是防御纵深:config.Normalize 本来就不允许加载/保存"没有 key 又不匿名"的配置,所以线上走不到,但它让问题列表和就绪标志说同一句话。
  • 超过 1 MiB 横幅不再算失败:以前这次尝试失败、observeKeyResult/observeAnonymousResult 记一次失败并冷却该 key/出口,然后换地方重试;现在这次尝试算成功,key/出口被标记健康。
  • Retry-After 仍被尊重,只是最多 8 × performance.failure_cooldown_seconds(默认 15 即 120 秒)。真耗尽的 key 会再次 429,代价是多一次尝试,而不是给出错误答案。
  • 尝试编号在这条基线上是纯重构:还没有调用方传非 0 偏移(两个调用点都传 0),但它消除了"调用方真的带偏移之后会出现的重复计数"。
  • 没有移植的东西:原提交里配置"保存表单"那段没有带过来——它持久化的作者填的 logging.file_path 和 proxy_by_model,而这两个字段在本基线上不存在,移植会拖进日志落盘和按模型代理池两个特性,与本 PR 无关。

验证

WSL(Ubuntu-24.04,go1.27.2 linux/amd64)下 gofmt -l . 无输出,go build ./...、go vet ./...、go test -count=1 ./... 全绿。

手工验证过的行为:

  • 坏地址(8080、localhost、http://localhost:8080、::1:8080、127.0.0.1:99999)启动即失败、退出码 1,日志里只有一行 invalid_listen_address。
  • 声明 Content-Length: 1000 却只发一半的请求,在 57.5 秒左右被读超时断开并返回,不再无限挂住。
  • 没有 key、没开匿名通道的配置下 /health 返回 503、ready: false。
  • 上游 Retry-After: 3600 时,key 只被挂起 120 秒(默认冷却 15 秒 × 8)。

不包含测试文件,与仓库现状一致。

配套前端:配置中心与诊断页里对应的控件单独放在 pr/webui-config 分支的前端 PR(本仓库 #10:https://github.com/Meepo268/opencode2api/pull/10),等这一系列后端合入后对上游提交,不在本 PR 范围内。

…ling, attempt numbering)

Ported from 1a13f24. The original commit also persisted the authored config form (logging.file_path, proxy_by_model) instead of the normalized one; neither field exists on this base, so that hunk is omitted rather than dragged in with the feature code it depends on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant