mirror of
https://github.com/coredns/coredns.git
synced 2026-04-05 11:45:33 -04:00
Add an atomic.Bool to singleflight prefetching (#7963)
Also updated plugin to document single-flighting Signed-off-by: Ryan Brewster <rpb@anthropic.com>
This commit is contained in:
20
plugin/cache/handler.go
vendored
20
plugin/cache/handler.go
vendored
@@ -55,13 +55,11 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
|
||||
// Adjust the time to get a 0 TTL in the reply built from a stale item.
|
||||
now = now.Add(time.Duration(ttl) * time.Second)
|
||||
if !c.verifyStale {
|
||||
cw := newPrefetchResponseWriter(server, rc, do, cd, c)
|
||||
go c.doPrefetch(ctx, cw, i, now)
|
||||
c.tryPrefetch(ctx, i, server, rc, do, cd, now)
|
||||
}
|
||||
servedStale.WithLabelValues(server, c.zonesMetricLabel, c.viewMetricLabel).Inc()
|
||||
} else if c.shouldPrefetch(i, now) {
|
||||
cw := newPrefetchResponseWriter(server, rc, do, cd, c)
|
||||
go c.doPrefetch(ctx, cw, i, now)
|
||||
c.tryPrefetch(ctx, i, server, rc, do, cd, now)
|
||||
}
|
||||
|
||||
if i.wildcard != "" {
|
||||
@@ -91,6 +89,20 @@ func wildcardFunc(ctx context.Context) func() string {
|
||||
}
|
||||
}
|
||||
|
||||
// tryPrefetch dispatches a background prefetch for i if one is not already in
|
||||
// flight. The CAS on i.refreshing ensures at most one prefetch goroutine per
|
||||
// item, so prefetch load scales with distinct stale keys rather than QPS.
|
||||
func (c *Cache) tryPrefetch(ctx context.Context, i *item, server string, req *dns.Msg, do, cd bool, now time.Time) {
|
||||
if !i.refreshing.CompareAndSwap(false, true) {
|
||||
return
|
||||
}
|
||||
cw := newPrefetchResponseWriter(server, req, do, cd, c)
|
||||
go func() {
|
||||
defer i.refreshing.Store(false)
|
||||
c.doPrefetch(ctx, cw, i, now)
|
||||
}()
|
||||
}
|
||||
|
||||
func (c *Cache) doPrefetch(ctx context.Context, cw *ResponseWriter, i *item, now time.Time) {
|
||||
// Use a fresh metadata map to avoid concurrent writes to the original request's metadata.
|
||||
ctx = metadata.ContextWithMetadata(ctx)
|
||||
|
||||
Reference in New Issue
Block a user