Repository navigation
fix(api): 支持 Retry-After 的 HTTP-date 形态 - #26
Merged
Merged
Conversation
RFC 9110 允许 Retry-After 用秒数或 HTTP-date。旧实现直接 Number() 解析,HTTP-date 得 NaN 后退回抖动退避,可能在窗口过去前就去撞限流。现在两种形态都解析,已经过去的时刻按最短等待处理。
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.
问题
RFC 9110 规定
Retry-After可以是秒数,也可以是 HTTP-date。app/src/services/api.ts里直接Number(raw):NaN→ 判定「没有可用的 Retry-After」,退回 ±25% 抖动的指数退避;做法
新增
parseRetryAfterMs(raw):<= 0视为无效;Date.parse可解析 →max(at - Date.now(), 1000),即「距该时刻还剩多久」,已经过去的时刻给最小等待 1 秒;null,仍走退避。结果仍然受
MAX_RETRY_DELAY_MS(10s)上限约束。测试
app/src/services/__tests__/api.test.ts新增两条:Retry-After: Wed, 25 Nov 2026 08:00:03 GMT且当前时间为08:00:00→ 2.999s 时还没重试,3s 后重试成功;07:59:00)→ 按 1 秒最短等待重试。原有「秒数」与「body 里的
retry_after」两条用例保持通过。验证
cd app && npx vitest run src/services/__tests__/api.test.ts→ 21 passedcd app && npm test→ 172 passedcd app && npm run lint→ 无输出