Skip to content

feat(provider): 支持单个模型商配置多个 API Key 做负载均衡 (closes #104) - #113

Merged
LAGcomcom merged 1 commit into
LAGcomcom:masterfrom
ghshhf:feat/104-multi-key-lb
Oct 23, 2026
Merged

LAGcomcom merged 1 commit into
LAGcomcom:masterfrom
ghshhf:feat/104-multi-key-lb

Conversation

@ghshhf

@ghshhf ghshhf commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

解决的问题

issue #104:当前每个模型商只能填一个 API Key,要多 key 只能重复添加同一模型商,还不能做负载均衡。本 PR 让单个模型商可填多个 Key,请求时在它们之间做负载均衡 + 失败冷却。

改动

  • store.Provider 新增 APIKeys []string,并提供 EffectiveKeys() 合并去重(旧字段 APIKey 仍兼容)。
  • 新增 internal/relay/balancer.go:轮询选 key,按失败类型冷却——凭据错(401/403) 10 分钟、限流(429) 10 秒、5xx 15 秒;全部冷却时取最早到期者,避免彻底饿死。
  • relay.postStreamed 用选中的 key 发请求,出错按 lane.ClassifyFailure 的 code 冷却对应 key。
  • 管理端 providerIn 支持 apiKeys,保存时归一化(合并后只有 1 个 → 写回 APIKey,多个 → 写 APIKeys);列表视图返回 keyCount。
  • 内置前端 web/index.html(已 go:embed 进二进制):把单个密码框换成多行文本框,按行拆分多个 Key,卡片显示「N 个 Key 负载均衡」。

兼容性

  • 旧单 key 配置(APIKey)完全保留;EffectiveKeys() 会把旧字段并入。
  • 探针 / 模型目录拉取走 firstKey(),单 key 路径行为不变。

测试

新增 balancer_test.go / lb_e2e_test.go(TestCompleteLoadBalancesKeys) / provider_keys_test.go,覆盖轮询、冷却、去重、归一化保存;go build ./... 与 store/relay/gateway/subs/lane 全部测试通过。

Closes #104

一个供应商可填写多个 Key,请求在它们之间轮询分摊,并自动
避开失效(401/403)或被限流(429/5xx)的 Key。
- store.Provider 新增 APIKeys 字段与 EffectiveKeys 合并方法
- relay 新增负载均衡器:round-robin 选 Key + 失败冷却
- 转发/探测路径接入多 Key;管理 API 与 dashboard 表单同步支持
- 向后兼容:单 Key 仍走 APIKey 字段,配置/导出均可读

Co-Authored-By: WorkBuddy <workbuddy@tencent.com>
@LAGcomcom
LAGcomcom merged commit 656afc7 into LAGcomcom:master Oct 23, 2026
3 checks passed
LAGcomcom added a commit that referenced this pull request Oct 23, 2026
This reverts commit 656afc7, reversing
changes made to dc5a90a.
LAGcomcom added a commit that referenced this pull request Oct 23, 2026
Lands the ideas reviewed from #113-#116 as our own implementations, with
tests written against the failure shapes rather than the patches:

- usage: streaming usage snapshots fold per-field max instead of summation,
  which compounded repeated terminal frames up to 1400x (issue #105). The
  relay path shares the aggregation, and OpenAI's nested
  prompt_tokens_details.cached_tokens is now read (flat keys still win).
- hidden models (issue #103): user-unchecked ids stop consuming probe rounds
  and stop acting as failover/throttle-switch candidates while staying
  directly callable; the set is seeded on boot and re-synced on visibility
  toggles and imports. An all-hidden round no longer reads as a quota wall.
- dead egress: a tunnel-level failure (transport/timeout/reset) now books
  the node, not just the (exit, model) pair — two strikes inside the
  fail-memory window retire it from rotation until the dead-node probe
  answers; a raced success still resurrects it, and a probe revival clears
  the strike counter.
- providers accept several API keys with per-request round-robin and classed
  cooldowns (credential 10m, 429 retry-after/10s, 5xx 15s; issue #104).
  Keys are write-only from the panel, masked in state and exports, and the
  single-key field stays the storage shape for one-key providers.
@LAGcomcom

Copy link
Copy Markdown
Owner

感谢贡献!多 Key 负载均衡确实是刚需。已按这个思路实现并合入 7eb328b:providers 支持多 Key 按请求轮换,失败按类别冷却(凭据拒绝 10 分钟、429 按 Retry-After、5xx 15 秒),面板支持多行输入、状态与导出均做掩码处理,单 Key 的存储形态保持不变以兼容旧版本。欢迎使用并反馈!

ghshhf pushed a commit to ghshhf/zen-gate that referenced this pull request Nov 3, 2026
上游把我们的 LAGcomcom#113-LAGcomcom#116、probe-verdicts 与 LAGcomcom#79 全部 revert 后按自己思路重写
(7eb328b / 0ae4c0b / 97eb4f3)。核对结果是重写版为我们需求的重写超集:
usage 累加、hidden 探测、dead-egress、多 key 轮询、404 误判同样都修了,
另外还覆盖嵌套 cached_tokens、Kilo 判定过期、CodeArts 400、探测进度
(LAGcomcom#117)、渠道墙、前缀缓存、ZCode/TRAE/Ext 账号池、面板重构、macOS sing-box
资产选择、无头编译期隔离。

冲突处理:17 个冲突文件以上游为基线。依据是自研版在这些点上均被超集覆盖,
保留自研实现等于丢功能,而非"优先自己"能保住的东西——真要自主,从这次
合并之后的新功能开始(后续提交见)。

顺带清理:删除 internal/lane/hidden_test.go 与 usage_test.go,它们测的是
旧 []string 语义;上游 hidden_skip_test.go / http_usage_max_test.go 已等价
且更完整地覆盖同一行为。

验证:go build ./... 通过;go vet ./... 通过;lane/gateway/relay/subs/store
五个包测试全绿。
@ghshhf
ghshhf deleted the feat/104-multi-key-lb branch November 3, 2026 14:49
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.

希望可以加入一个模型商可以填多个api key,让负载均衡一下,现在多个key需要添加多次,还不能负载

2 participants