httpcache: caching http.RoundTripper

Open
#128 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
backend

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

enhancement

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, no Range, no Authorization/Cookie unless 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-Control private, no-store or no-cache; an unsafe Vary; 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 wraps context.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 roughly WithMaxBodySize, and closing that body closes the upstream body.
  • Cache status: with WithCacheStatusHeader(name), the returned response carries HIT, MISS or BYPASS in that header. Stored responses also get Age.
  • Stored responses come back as a normal *http.Response: Request is the caller's request, ContentLength is set, and the body is readable and closable. Hop-by-hop headers are not stored.

Acceptance criteria

  • Two sequential GETs through an http.Client using the transport make one upstream call; the second response is HIT with the same status, headers and body, and has Age.
  • 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-GET method; a response with Set-Cookie, no-store/private/no-cache or an unsafe Vary. Requests with Authorization/Cookie are cached with WithAllowAuthorized/WithAllowCookies.
  • A cancelled caller returns its context error promptly, while a concurrent caller on the same key still gets the response. A WithTimeout expiry returns an error wrapping context.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.Request is 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-lint and fieldalignment are clean.
  • Package docs (the httpcache package doc and the NewTransport / Transport docs) 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, detach context helper and the collapsing/pass-marker flow through CacheC. Don't duplicate them. Small refactors that let both share them are in scope.
  • The Cache interface the middleware already accepts is the dependency; *anycache.Cache satisfies it.
  • Assumption: the base transport is set with a new option, WithTransport(http.RoundTripper), defaulting to http.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.
  • WithWarmUpTTL is handled in the follow-up issue. Until then, the transport documents that it is not supported, and either rejects it in NewTransport or 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ksysoev/anycache

All issues in ksysoev/anycache

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.