Skip to content

refactor(proxy): return remaining header from rateLimitSignal to eliminate duplicate read#9053

Merged
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-x-ratelimit-remaining
Jul 10, 2026
Merged

refactor(proxy): return remaining header from rateLimitSignal to eliminate duplicate read#9053
lpcox merged 2 commits into
mainfrom
copilot/duplicate-code-x-ratelimit-remaining

Conversation

Copilot AI commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

X-Ratelimit-Remaining was read twice in the same call chain: once inside rateLimitSignal and again in its caller injectRetryAfterIfRateLimited. This creates a latent inconsistency risk if the header name is ever changed in one place but not the other.

Changes

  • rateLimitSignal: Extended return signature from (bool, string) to (bool, string, string) — now returns (isRateLimited, resetHeader, remaining)
  • injectRetryAfterIfRateLimited: Removed redundant resp.Header.Get("X-Ratelimit-Remaining") call; uses remaining from rateLimitSignal directly
  • Tracing call site: Updated to discard the new third return value with _
  • Tests: Added assertions on the remaining return value in all three TestRateLimitSignal subtests
// Before
func rateLimitSignal(resp *http.Response) (bool, string) { ... }
// injectRetryAfterIfRateLimited called rateLimitSignal then read the header again

// After
func rateLimitSignal(resp *http.Response) (bool, string, string) {
    remaining := resp.Header.Get("X-Ratelimit-Remaining")
    return is429 || remaining == "0", resp.Header.Get("X-Ratelimit-Reset"), remaining
}

…oxy handler

Update rateLimitSignal to return remaining as a third return value, and
update injectRetryAfterIfRateLimited to use it directly instead of
re-reading the header. Update all callers and tests accordingly.
Copilot AI changed the title [WIP] Fix duplicate reading of X-Ratelimit-Remaining header in proxy handler refactor(proxy): return remaining header from rateLimitSignal to eliminate duplicate read Jul 10, 2026
Copilot finished work on behalf of lpcox July 10, 2026 11:24
Copilot AI requested a review from lpcox July 10, 2026 11:24
@lpcox lpcox marked this pull request as ready for review July 10, 2026 11:37
Copilot AI review requested due to automatic review settings July 10, 2026 11:37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the proxy rate-limit detection path to avoid reading X-Ratelimit-Remaining twice by returning the parsed “remaining” value from rateLimitSignal and reusing it in injectRetryAfterIfRateLimited. This reduces duplication and keeps the rate-limit header handling consistent within the call chain.

Changes:

  • Extended rateLimitSignal to return (isRateLimited, resetHeader, remaining) and updated call sites accordingly.
  • Removed redundant resp.Header.Get("X-Ratelimit-Remaining") in injectRetryAfterIfRateLimited and reused the returned remaining.
  • Updated unit tests to assert the new remaining return value across all TestRateLimitSignal subtests.
Show a summary per file
File Description
internal/proxy/handler.go Updates rateLimitSignal signature and eliminates the duplicate header read in injectRetryAfterIfRateLimited; updates tracing call site to ignore the new return value.
internal/proxy/rate_limit_test.go Updates tests to capture and assert the new remaining return value from rateLimitSignal.

Review details

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Low

@lpcox lpcox merged commit e474760 into main Jul 10, 2026
39 checks passed
@lpcox lpcox deleted the copilot/duplicate-code-x-ratelimit-remaining branch July 10, 2026 15:22
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.

[duplicate-code] Duplicate Code Pattern: X-Ratelimit-Remaining header read twice in proxy handler

3 participants