httpcache: caching http.RoundTripper
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
Research direction
Start by reading the existing httpcache middleware, especially its key building, bypass policy, response classification, entry encoding, detach helper and CacheC collapsing/pass-marker flow; the issue names no files or tests. Add the transport in the httpcache package and put tests next to the middleware tests. Done means the listed caching, concurrency, cancellation, storage failure and large-body acceptance criteria pass, package and API docs cover supported options and SSE/long-polling limits, and lint and race checks are clean.
Written by the indexing model from the issue text.
Description
What to build
A caching http.RoundTripper in the httpcache package: httpcache.NewTransport(cache, ttl, opts...) (*Transport, error). With it, an http.Client gets anycache caching and request collapsing with no other code. It wraps a base transport, by default http.DefaultTransport, and accepts the existing httpcache options.
From the caller's side:
- Cacheable request (
GET, no body, noRange, noAuthorization/Cookieunless opted in, no caller-set conditional headers, not an Upgrade or event-stream request):- The first call goes upstream; the response is stored if the store/share/pass policy allows it.
- Later calls within the TTL get the stored response without contacting upstream.
- Concurrent identical requests make a single upstream call and each receive their own copy of the response: status, headers, and a body that can be read independently.
- Non-cacheable statuses (for example a 500) are shared too.
- Private responses (
Set-Cookie;Cache-Controlprivate,no-storeorno-cache; an unsafeVary; an oversized body) are returned only to the caller that made the call; each other caller makes its own call.
- Non-cacheable requests go straight to the base transport, unchanged.
- Transport errors (DNS, connection refused, TLS) are returned to the caller as errors. They are shared with the callers waiting at that moment and never stored.
- Storage errors fail open: if the cache can't be read, the request goes upstream directly. The upstream call is never made twice for one
RoundTrip. - Cancellation: a caller whose context ends gets its context error at once. The shared upstream call carries on for the others, keeping the caller's context values, bounded by
WithTimeout. A timeout gives every waiting caller an error that wrapscontext.DeadlineExceeded. - Large bodies: once a body outgrows
WithMaxBodySize, the caller that made the call receives a response whose body is the buffered part followed by the rest of the live upstream body. It is not stored or shared, memory stays bounded by roughlyWithMaxBodySize, and closing that body closes the upstream body. - Cache status: with
WithCacheStatusHeader(name), the returned response carriesHIT,MISSorBYPASSin that header. Stored responses also getAge. - Stored responses come back as a normal
*http.Response:Requestis the caller's request,ContentLengthis set, and the body is readable and closable. Hop-by-hop headers are not stored.
Acceptance criteria
- Two sequential
GETs through anhttp.Clientusing the transport make one upstream call; the second response isHITwith the same status, headers and body, and hasAge. - N concurrent identical
GETs make one upstream call and each read the full body; a concurrent upstream 500 is shared, not stored. - Each of these goes upstream on every call and is never stored: a request with
Authorization,Cookie,Range, a body or a conditional header; a non-GETmethod; a response withSet-Cookie,no-store/private/no-cacheor an unsafeVary. Requests withAuthorization/Cookieare cached withWithAllowAuthorized/WithAllowCookies. - A cancelled caller returns its context error promptly, while a concurrent caller on the same key still gets the response. A
WithTimeoutexpiry returns an error wrappingcontext.DeadlineExceeded. - A storage read failure still returns the upstream response (one upstream call), and a warning is logged through
WithLogger. - A body much larger than
WithMaxBodySize(for example 100× the limit) is received in full by the caller, is not stored, and the transport's buffering stays within the limit (tested on the transport's buffer, not on process memory). - The caller's
*http.Requestis never mutated (RoundTripper contract), checked in a test. - Transport and middleware keys never collide when one store backs both.
- Tests use testify, table-driven where it fits, next to the middleware tests, and pass with
-race.golangci-lintandfieldalignmentare clean. - Package docs (the
httpcachepackage doc and theNewTransport/Transportdocs) describe the transport, which options apply to it, and that it must not be used for SSE or long-polling calls.
Notes
- Reuse the middleware's key building, request bypass policy, response classification, entry encoding,
detachcontext helper and the collapsing/pass-marker flow throughCacheC. Don't duplicate them. Small refactors that let both share them are in scope. - The
Cacheinterface the middleware already accepts is the dependency;*anycache.Cachesatisfies it. - Assumption: the base transport is set with a new option,
WithTransport(http.RoundTripper), defaulting tohttp.DefaultTransport. - Assumption: the transport uses a key prefix distinct from the middleware's, so they never read each other's entries.
- Assumption: caller-set conditional headers (
If-None-Match,If-Modified-Since, ...) make the request bypass the cache, since the caller expects to handle a 304 itself. - Assumption: the stored body is what the base transport returned. When Go's transport added gzip itself, that body is already decompressed.
WithWarmUpTTLis handled in the follow-up issue. Until then, the transport documents that it is not supported, and either rejects it inNewTransportor ignores it. The implementer decides which and documents it.
- Dominant language
- Go
- Stars
- 1
- Forks
- 0
- Avg merge
- 4d 22h
- Merged PRs (30d)
- 5
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ksysoev/anycache
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 53/100
-
Refactor cache to be generic functionMay be free again @ksysoev claimed this 149 days ago, and no pull request is open. Open
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
All issues in ksysoev/anycache
Similar issues
-
Linux 主机 macOS 主机
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Lee-xeo/Claude-Cage#1455 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
slavakurilyak/awesome-ai-agents#1137 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
Make the HTTP server's keep-alive and headers timeouts configurablePossibly taken @mattgodbolt claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
compiler-explorer/compiler-explorer#9902 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day