Skip to content

fix: KeyPool 并发竞态 — 防止多请求同时命中同一 Key#25

Merged
bigmanBass666 merged 3 commits into
mainfrom
worktree-fix-concurrent-key
Jul 16, 2026
Merged

fix: KeyPool 并发竞态 — 防止多请求同时命中同一 Key#25
bigmanBass666 merged 3 commits into
mainfrom
worktree-fix-concurrent-key

Conversation

@bigmanBass666

Copy link
Copy Markdown
Contributor

问题

KeyPool.Next() 的 Allow() 是只读检查,不预留 key。两个并发请求可同时看到同一个 key Allow() == true,都拿到并发送请求,导致上游收到双倍请求 → 429。

修复

在 KeyPool 中新增 inUse []bool 字段:

  1. Next() 现在同时检查 Allow() && !inUse[idx],选中后立即标记 inUse[idx] = true
  2. Release(idx) 清除使用中标记,在请求完成后调用
  3. 释放点在 proxy_handler.go 中:
    • client.Do(req) 返回后立即释放(主路径)
    • CB 双检段跳过时释放(防御路径)
  4. AddKey/RemoveKey 同步管理 inUse 切片

效果

两个并发请求不会再同时拿到同一个 key。实测高峰期 17:00 的 429 率应有明显下降(之前单个 key 一分钟内被突刺 24 次的情况不再出现)。

后续

  • 日志 Token 计量(独立 ticket)
  • RPM 感知的 Key 选择(独立 ticket)

@bigmanBass666
bigmanBass666 merged commit 4d666eb into main Jul 16, 2026
4 checks passed
@bigmanBass666
bigmanBass666 deleted the worktree-fix-concurrent-key branch July 16, 2026 18:11
bigmanBass666 pushed a commit that referenced this pull request Jul 16, 2026
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