plugin/cache: cache SERVFAIL when the resolver fails without a response (#8629)

* plugin/cache: cache SERVFAIL when the resolver fails without a response

The forward plugin returns (SERVFAIL, err) on an upstream timeout without
calling WriteMsg, so the response never reaches the cache ResponseWriter
and is not stored. Every client retry then hits the upstream again and
pays the full forward timeout, while a SERVFAIL sent by the upstream is
cached normally.

Store a SERVFAIL generated from the failure, bounded by the existing
servfail TTL, so identical queries are answered from the cache while it is
valid. A servfail TTL of 0 keeps the previous behaviour.

Signed-off-by: Ahmad Mehrabi <mehrabi@post.com>

* plugin/cache: do not cache SERVFAIL caused by a canceled request

forward also returns SERVFAIL together with an error when the request
context is canceled. DoH propagates the HTTP request context, so caching
that failure would make every subsequent request with the same cache key
fail until the servfail TTL expires, even though the upstream is healthy.

Only insert the synthetic SERVFAIL when the request context is still live,
and add a regression test that cancels the first request and then verifies
that a subsequent healthy request reaches the backend.

Signed-off-by: Ahmad Mehrabi <mehrabi@post.com>

---------

Signed-off-by: Ahmad Mehrabi <mehrabi@post.com>
This commit is contained in:
Ahmad Mehrabi
2026-10-16 05:59:41 +03:30
committed by GitHub
parent 3909cc187c
commit 16b53a46c1
3 changed files with 141 additions and 1 deletions

11
plugin/cache/cache.go vendored
View File

@@ -481,6 +481,17 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
return w.ResponseWriter.WriteMsg(res)
}
// cacheServerFailure stores a SERVFAIL response for req in the cache without
// writing it to the client. It is used when the next plugin fails without
// producing a response, so the failure can be served from the cache while the
// servfail TTL is valid.
func (w *ResponseWriter) cacheServerFailure(req *dns.Msg) {
resp := new(dns.Msg)
resp.SetRcode(req, dns.RcodeServerFailure)
w.prefetch = true
_ = w.WriteMsg(resp)
}
func (w *ResponseWriter) set(m *dns.Msg, key uint64, mt response.Type, duration time.Duration) {
// duration is expected > 0
// and key is valid