Skip to content

plugin/cache: prefetch without holding a client connection#7944

Merged
yongtang merged 1 commit into
coredns:masterfrom
rpb-ant:rpb/extract-client-conn
Mar 24, 2026
Merged

plugin/cache: prefetch without holding a client connection#7944
yongtang merged 1 commit into
coredns:masterfrom
rpb-ant:rpb/extract-client-conn

Conversation

@rpb-ant

@rpb-ant rpb-ant commented Mar 18, 2026

Copy link
Copy Markdown
Contributor

1. Why is this pull request needed and what does it do?

newPrefetchResponseWriter currently captures the client's ResponseWriter and does some work to ensure that RemoteAddr is safe even if the client's connection is closed by the time the prefetch goroutine runs.

This PR more strongly decouples the newPrefetchResponseWriter from the client.

  • This is theoretically safer: the embedded writer is still reachable via the un-overridden interface methods (LocalAddr, etc), and if anyone happened to use those methods they'd potentially misbehave
  • it is also prep work for a change that I'd like to make to allow for totally independent background prefetches, decoupled from any specific client request

2. Which issues (if any) are related?

#7904

In particular, right now the cache plugin's prefetch behavior requires a client to make a request in order to trigger a pre-fetch. If there are a ton of client requests, it can end up firing a ton of prefetch requests.

One solution to this problem is to de-duplicate the upstream fetches. That seems reasonable, but there are some tricky aspects of it.

Here what I'm proposing is that we allow actually decouple prefetching from client requests entirely. This PR does not make that change, it's just prep-work.

My intent here is for this PR to be easy to review, but also to lay the groundwork for a subsequent PR.

3. Which documentation changes (if any) need to be made?

this is a an internal refactor, I don't believe this requires any docs changes

4. Does this introduce a backward incompatible change or deprecation?

prefetch requests no longer carry the triggering client's address (intentionally)

Downstream plugins see a zero TCP addr. I don't think this is impactful (I couldn't find a plugin after cache that reads it) but...in theory this could be a breaking change? Seems unavoidable if I also want to initiate requests that don't have an associated clients.

@rpb-ant rpb-ant force-pushed the rpb/extract-client-conn branch from 7a3534b to b708c35 Compare March 18, 2026 18:43
@rpb-ant

rpb-ant commented Mar 18, 2026

Copy link
Copy Markdown
Contributor Author

LMK if you want me to open an Issue to discuss the concrete proposal. I can also open the implementation PR so you can see concretely what I'm driving towards if that's helpful.

newPrefetchResponseWriter previously captured the client's ResponseWriter
and cached its RemoteAddr at construction time, noting that the connection
might already be closed by the time the prefetch goroutine runs. The
embedded writer remained reachable via the un-overridden interface methods
(LocalAddr, Close, TsigStatus, TsigTimersOnly, Hijack). Nothing on the
prefetch path calls those today, which is why this hasn't bitten, but
that's luck rather than design.

This makes the cache's ResponseWriter a complete dns.ResponseWriter on its
own: the five previously-delegating methods get nil-safe overrides, and
newPrefetchResponseWriter constructs a writer with no inner RW.

doPrefetch and exists previously took a request.Request and re-derived
do/cd via state.Do(), which traverses state.W. They now take the
primitives directly from cw (where they already live) so state.W can be
nil.

Signed-off-by: Ryan Brewster <rpb@anthropic.com>
@rpb-ant rpb-ant force-pushed the rpb/extract-client-conn branch from b708c35 to b8e9bd2 Compare March 18, 2026 19:18
@yongtang yongtang merged commit 31e1602 into coredns:master Mar 24, 2026
11 checks passed
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.

2 participants