Skip to content

Avoid caching server error responses - #354

Merged
brettfo merged 3 commits into
mainfrom
dev/brettfo/cache-errors
Nov 6, 2026
Merged

brettfo merged 3 commits into
mainfrom
dev/brettfo/cache-errors

Conversation

@brettfo

@brettfo brettfo commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

What are you trying to accomplish?

Prevent transient server failures, such as a 503, from being replayed indefinitely by the proxy cache instead of allowing a subsequent client retry to reach upstream.

Responses with status codes 500 and above are no longer cached. Previously recorded responses in that range are discarded on lookup so a recovered response can replace them. Responses below 500 remain cacheable, subject to the existing 101 and 202 exclusions.

Anything you want to highlight for special attention from reviewers?

The status-code policy lives in shouldCacheResponse(statusCode int) bool and applies without an experiment flag or CLI changes. This does not introduce automatic retries; it lets subsequent client retries reach upstream.

The persisted-cache regression test exposed unclosed db.yaml handles on Windows. The cache now closes the metadata file after loading or writing it and propagates write-close errors.

How will you know you've accomplished your goal?

Regression tests cover GET and HEAD responses across the 499/500 boundary, previously persisted 404/503 entries, and proxy requests showing that a retry after a 503 reaches upstream and the recovered 200 is cached while a 404 remains cached.

Validation on Windows:

  • go test .\internal\cache . -count=1 — passed.
  • go vet .\internal\cache . — passed.
  • git diff --check — passed.

The complete Docker/race test suite (script/test) and full-repository linters were not run.

Checklist

  • I have run the complete test suite to ensure all tests and linters pass.
  • I have thoroughly tested my code changes to ensure they work as expected, including adding additional tests for new functionality.
  • I have written clear and descriptive commit messages.
  • I have provided a detailed description of the changes in the pull request, including the problem it addresses, how it fixes the problem, and any relevant details about the implementation.
  • I have ensured that the code is well-documented and easy to understand.

Skip caching and replaying responses with status codes 500 and above so client retries can reach upstream. Preserve client-error caching and cover recovery from persisted server errors.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 08d569d9-063d-4acb-b51f-90b2bc12fb4e
@brettfo
brettfo requested a review from a team as a code owner November 4, 2026 20:14
Copilot AI balanced review requested due to automatic review settings November 4, 2026 20:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical filename-allocation collision after eviction and restart can truncate a retained cached response’s body when a new response is written.

1 open issue
What changed in this PR

This PR prevents the proxy cache from replaying transient server failures, allowing subsequent client retries to reach upstream.

Changes:

  • Skip caching responses with status codes 500 and above, and discard previously cached entries in that range.
  • Close cache metadata files after loading or writing, and propagate write-close errors.
  • Add regression coverage for status boundaries, persisted entries, and recovery after a 503.
File Description
proxy_test.go Tests upstream recovery after a 503 and continued caching of 404 responses.
internal/​cache/​handlers.go Applies the server-error cache policy and closes metadata file handles.
internal/​cache/​handlers_test.go Tests GET/HEAD status boundaries and replacement of persisted server errors.

Review effort: Balanced

Comment thread internal/cache/handlers.go Resolved
Create cache body files exclusively and retry existing filenames to preserve retained and orphaned files. Add a regression covering eviction, reload, and subsequent cache writes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 08d569d9-063d-4acb-b51f-90b2bc12fb4e

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the change to prevent the proxy cache from replaying transient server failures.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Comment thread internal/cache/handlers.go Outdated
@@ -319,6 +329,9 @@ func (d *DB) OnResponse(resp *http.Response, proxyCtx *goproxy.ProxyCtx) *http.R
if resp.StatusCode == http.StatusAccepted {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor, but can we put this if into shouldCacheResponse? That makes a nice place to put these sort of things going forward.

Move the HTTP 202 exclusion into shouldCacheResponse and cover new and previously recorded accepted responses.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 08d569d9-063d-4acb-b51f-90b2bc12fb4e
@brettfo
brettfo merged commit eece1f7 into main Nov 6, 2026
112 checks passed
@brettfo
brettfo deleted the dev/brettfo/cache-errors branch November 6, 2026 19:32
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.

3 participants