Repository navigation
fix(ratelimit): make in-memory Retry-After cover an empty minute bucket - #2489
Conversation
When the per-second bucket refused a request, MemoryLimiter returned its delay without checking the per-minute bucket. With both empty, a client that honored Retry-After was refused again. Report the longer of the two waits, reading the minute bucket without taking a token, and round the reset time up to a whole second. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
Warning Review limit reachedYou've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Next included review available in 10 minutes. View limit details
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
The 429 response headers change and the Evidence section does not show them:
Automated check: Macroscope check run agent (gpt-6-luna). Evidence was not reviewed for correctness. Posted via Macroscope — Visible change evidence |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d7ebea7158
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
Quick104
left a comment
There was a problem hiding this comment.
Adversarial review (gpt-6.1-sol, reviewed d7ebea7)
[P2] Round up the minute wait in the refresh response paths — internal/api/handlers/catalog_actions_service.go:90 and :260
The person and trailer refresh handlers still convert result.RetryAfter with max(1, int(result.RetryAfter.Seconds())). With their normal limit of 10 requests/second, 10/minute, burst 10, send ten requests 10ms apart and the next at T+91ms. The new minute check returns about 5.909s, but both handlers emit Retry-After: 5. At T+5.091s the minute bucket has only about 0.8485 tokens, so a client honoring that header receives another 429. Both the v1 writer and the v2 serviceProblem adapter forward the truncated integer unchanged. This rounding predates the PR, but it leaves the reported retry problem unresolved on these two affected callers. Round positive durations up at both conversions and test the emitted header with a fractional minute wait; the new tests exercise AllowResult directly and cannot catch this.
Verified from the source and an arithmetic check of the bucket updates. Go tests and live deployment validation were unavailable.
AI disclosure: Codex, gpt-6.1-sol.
|
Suggested line: Automated check: Macroscope check run agent (gpt-6-luna). No validation was performed. Posted via Macroscope — v1 validation impact |
…delay When the minute bucket's wait saturates to rate.InfDuration, adding a second to round it up overflowed and set X-RateLimit-Reset in the past. Round the time instead, which saturates. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
Problem
Closes #2486
Related issue: #2483
Validation tasks: none
When the in-memory rate limiter refuses a request because a key has used up both its per-second and its per-minute budget,
Retry-Aftergives only the per-second wait. A client that waits that long is refused again, and only the second 429 carries the real wait for the minute bucket. This affects every limit served byMemoryLimiter: request rate limits when Redis isn't configured (orratelimit.backend = memory), and the person and trailer refresh limits, which always use it.MemoryLimiter.Allowreturned as soon as the per-second bucket refused, without looking at the minute bucket. It now reports the longer of the two waits.Approach
When the per-second bucket refuses, the limiter reads how long the minute bucket needs to hold a whole token and uses that wait if it is longer. It reads the bucket with
TokensAt, which takes no token. Reserving and cancelling instead, as the minute check further down does, can lose tokens when refused requests on one key overlap, so the new check avoids it. When the minute wait wins,ResetAtis that time rounded up to a whole second, becauseX-RateLimit-Resetcarries whole seconds and a floored value would send a client back early.Allownow calls anallowAtthat takes the current time, so the tests use fixed instants instead of sleeping.No contract or schema changes. Clients see a longer
Retry-AfterandX-RateLimit-Reseton this one kind of refusal, which is the correct value. The Redis limiter has the same bug and is fixed separately in #2485; this PR doesn't touchredis.go.Validation
Retry-Aftercovers the minute bucket's 30s, that a request after that wait is allowed, and that a client retrying at theX-RateLimit-Resetsecond from mid-second is allowed. Before the fix it failed with a 1sRetry-After. With the fix but without the rounding, the reset check failed. A second test checks that a per-second refusal with room in the minute bucket still waits 1s, and a third that a minute rate too slow for aDurationsaturatesRetry-Afterwithout puttingX-RateLimit-Resetin the past.go build ./...,gofmt,go vet ./...,golangci-lint --new-from-patch, andmake test-gopass. The ratelimit tests also pass with-race.Evidence
Evidence: none, no user-visible change
Risks
None identified. Refused clients wait longer only when the minute bucket is also empty, which is the wait they would get on their next attempt anyway.
Found in review, predating this PR, and filed separately. The first two change lines this PR changes, so their fixes should follow it.
CancelAt, so a shared key stays throttled longer than configured.X-RateLimit-Resetis always the next second, even when the wait is several seconds (sign-in limits), and the per-minute branch's is floored.Retry-Afterdown, so a 5.9s wait becomes 5s. That is outside the limiter; this PR makes those fractional waits come up more often.Checklist
AI Disclosure
🤖 Generated with Claude Code
Note
Make in-memory
MemoryLimiterRetry-After cover an empty minute bucketMemoryLimiter.Allowto report aRetryAfterthat also accounts for an empty or slow per-minute bucket, not just the per-second wait. When the minute wait is longer,ResetAtis rounded up to a whole secondMemoryLimiter.allowAtwith an explicit timestamp, and adds atokenDelayhelper that computes the minute limiter's wait without consuming a tokenAllowsee longer (combined)RetryAftervalues and rounded-upResetAtwhen the minute bucket is empty; unrepresentable minute delays reportrate.InfDuration. See memory.goMacroscope summarized a2f392f.