Repository navigation
Avoid caching server error responses - #354
Merged
Merged
Conversation
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
There was a problem hiding this comment.
🟡 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
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
jakecoffman
reviewed
Nov 6, 2026
| @@ -319,6 +329,9 @@ func (d *DB) OnResponse(resp *http.Response, proxyCtx *goproxy.ProxyCtx) *http.R | |||
| if resp.StatusCode == http.StatusAccepted { | |||
Member
There was a problem hiding this comment.
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
jakecoffman
approved these changes
Nov 6, 2026
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.

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) booland 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.yamlhandles 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