aboutsummaryrefslogtreecommitdiff
path: root/plugin/cache/handler.go
diff options
context:
space:
mode:
authorGravatar sschepens <sebastian.schepens@mercadolibre.com> 2021-01-15 09:32:49 -0300
committerGravatar GitHub <noreply@github.com> 2021-01-15 12:32:49 +0000
commitb2a22eff04fbfd9801d865f8a7702d6f62dfac14 (patch)
tree3fa19b8a082e612954477d338ab06cac46606ce5 /plugin/cache/handler.go
parentcaaaf9ff3ea46ba40f0e01e19b1f7e56af933f01 (diff)
downloadcoredns-b2a22eff04fbfd9801d865f8a7702d6f62dfac14.tar.gz
coredns-b2a22eff04fbfd9801d865f8a7702d6f62dfac14.tar.zst
coredns-b2a22eff04fbfd9801d865f8a7702d6f62dfac14.zip
Prevent race from prefetching (#4368)
Automatically submitted.
Diffstat (limited to 'plugin/cache/handler.go')
-rw-r--r--plugin/cache/handler.go36
1 files changed, 16 insertions, 20 deletions
diff --git a/plugin/cache/handler.go b/plugin/cache/handler.go
index 350ec0bb0..2dd3c0646 100644
--- a/plugin/cache/handler.go
+++ b/plugin/cache/handler.go
@@ -39,39 +39,28 @@ func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
ttl = i.ttl(now)
}
if i == nil {
- if !do {
- setDo(rc)
- }
crr := &ResponseWriter{ResponseWriter: w, Cache: c, state: state, server: server, do: do}
- return plugin.NextOrFailure(c.Name(), c.Next, ctx, crr, rc)
+ return c.doRefresh(ctx, state, crr)
}
if ttl < 0 {
servedStale.WithLabelValues(server).Inc()
// Adjust the time to get a 0 TTL in the reply built from a stale item.
now = now.Add(time.Duration(ttl) * time.Second)
- addr := w.LocalAddr() // See https://github.com/coredns/coredns/issues/4271, unclear how, but pull this out of the goroutine, and get the address here.
- go func() {
- if !do {
- setDo(rc)
- }
- crr := &ResponseWriter{Cache: c, state: state, server: server, prefetch: true, remoteAddr: addr, do: do}
- plugin.NextOrFailure(c.Name(), c.Next, ctx, crr, rc)
- }()
+ cw := newPrefetchResponseWriter(server, state, c)
+ go c.doPrefetch(ctx, state, cw, i, now)
+ } else if c.shouldPrefetch(i, now) {
+ cw := newPrefetchResponseWriter(server, state, c)
+ go c.doPrefetch(ctx, state, cw, i, now)
}
resp := i.toMsg(r, now, do)
w.WriteMsg(resp)
- if c.shouldPrefetch(i, now) {
- go c.doPrefetch(ctx, state, server, i, now)
- }
return dns.RcodeSuccess, nil
}
-func (c *Cache) doPrefetch(ctx context.Context, state request.Request, server string, i *item, now time.Time) {
- cw := newPrefetchResponseWriter(server, state, c)
-
- cachePrefetches.WithLabelValues(server).Inc()
- plugin.NextOrFailure(c.Name(), c.Next, ctx, cw, state.Req)
+func (c *Cache) doPrefetch(ctx context.Context, state request.Request, cw *ResponseWriter, i *item, now time.Time) {
+ cachePrefetches.WithLabelValues(cw.server).Inc()
+ c.doRefresh(ctx, state, cw)
// When prefetching we loose the item i, and with it the frequency
// that we've gathered sofar. See we copy the frequencies info back
@@ -81,6 +70,13 @@ func (c *Cache) doPrefetch(ctx context.Context, state request.Request, server st
}
}
+func (c *Cache) doRefresh(ctx context.Context, state request.Request, cw *ResponseWriter) (int, error) {
+ if !state.Do() {
+ setDo(state.Req)
+ }
+ return plugin.NextOrFailure(c.Name(), c.Next, ctx, cw, state.Req)
+}
+
func (c *Cache) shouldPrefetch(i *item, now time.Time) bool {
if c.prefetch <= 0 {
return false