Repository navigation
Conversation
…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.
Suydev
added a commit
to Suydev/opencode2api
that referenced
this pull request
Nov 13, 2026
…nd stream budget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
摘要
两处硬化:启动参数写错时别再"优雅退出 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)net.ResolveTCPAddr("tcp", …)校验两个地址;失败分别打invalid_listen_address/invalid webui listen address(带address、error)并os.Exit(1)。webui.listen只在webui.enabled为真时校验,关掉的块里留着旧地址不会挡住启动。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 又不匿名"的配置,所以线上走不到,但它让问题列表和就绪标志说同一句话。observeKeyResult/observeAnonymousResult记一次失败并冷却该 key/出口,然后换地方重试;现在这次尝试算成功,key/出口被标记健康。Retry-After仍被尊重,只是最多8 × performance.failure_cooldown_seconds(默认15即 120 秒)。真耗尽的 key 会再次 429,代价是多一次尝试,而不是给出错误答案。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 秒左右被读超时断开并返回,不再无限挂住。/health返回 503、ready: false。Retry-After: 3600时,key 只被挂起 120 秒(默认冷却 15 秒 × 8)。不包含测试文件,与仓库现状一致。
配套前端:配置中心与诊断页里对应的控件单独放在
pr/webui-config分支的前端 PR(本仓库 #10:https://github.com/Meepo268/opencode2api/pull/10),等这一系列后端合入后对上游提交,不在本 PR 范围内。