feat(zhipu): 为 429 Rate Limit 添加指数退避重试挽回机制#242
Merged
Conversation
ZhipuVendor 遇到 HTTP 429 时自动重试最多 5 次(1 初始 + 4 重试), 采用指数退避 + Full Jitter 策略(1s → 2s → 4s → 8s), 优先尊重 server retry-after header 提供的恢复时间建议。 重试对 executor 完全透明,仅在全部重试耗尽后才触发 failover。 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
- 将 send_message_stream 中 yield 移至 try/except 外部,避免 async generator 意外捕获外部 athrow 抛入的 HTTPStatusError; - 删除 test_stream_429_retries_and_succeeds 中未生效的首段 mock 代码。 🤖 Generated with [Claude Code](https://github.com/claude), [CodeX](https://openai.com), [Gemini](https://github.com/apps/gemini-code-assist) Co-Authored-By: Aurelius Huang<threefish.ai@gmail.com>
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.
Summary
ZhipuVendor添加 HTTP 429 Rate Limit 专用重试挽回机制,max_attempt = 5(1 初始 + 4 重试),采用指数退避 + Full Jitter 策略(1s → 2s → 4s → 8s,上限 30s),优先尊重 serverretry-afterheader 提供的恢复时间建议send_message()(非流式)和send_message_stream()(流式),429 时自动重试,非 429 状态码直接透传不触发重试Motivation
当前 zhipu 遇到 429 时直接触发 failover,无法通过合理等待后自行恢复。429 是瞬态限流错误,通常在短时间内即可恢复,引入重试机制可让 zhipu 自救,避免过早切换到其他 vendor。
Implementation Details
routing/retry.py的RetryConfig+calculate_delay()(指数退避 + Full Jitter),以及routing/rate_limit.py的parse_rate_limit_headers()+compute_effective_retry_seconds()(server retry-after 解析)BaseVendor.send_message_stream()的 status code 检查阶段即 raise(在任何 data chunk yield 之前),重试不会导致已发出数据不一致Test Plan
uv run pytest tests/test_zhipu.py -v— 27 个测试全部通过(含 9 个新增 429 重试用例)uv run pytest tests/ -x -q— 全量 1459 测试通过,零回归🤖 Generated with Claude Code, CodeX, Gemini
Co-Authored-By: Aurelius Huangthreefish.ai@gmail.com