plugin/cache: don't cache msg with TTL=0 in them (#1116)
Don't cache these - may be lead to weird side effects. Fixes #1113
This commit is contained in:
parent
9d736fd754
commit
1e71d0e2c1
2 changed files with 33 additions and 3 deletions
5
plugin/cache/cache.go
vendored
5
plugin/cache/cache.go
vendored
|
@ -106,7 +106,7 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
|
|||
duration = msgTTL
|
||||
}
|
||||
|
||||
if key != -1 {
|
||||
if key != -1 && duration > 0 {
|
||||
w.set(res, key, mt, duration)
|
||||
|
||||
cacheSize.WithLabelValues(Success).Set(float64(w.pcache.Len()))
|
||||
|
@ -134,8 +134,7 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
|
|||
}
|
||||
|
||||
func (w *ResponseWriter) set(m *dns.Msg, key int, mt response.Type, duration time.Duration) {
|
||||
if key == -1 {
|
||||
log.Printf("[ERROR] Caching called with empty cache key")
|
||||
if key == -1 || duration == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
|
|
31
plugin/cache/cache_test.go
vendored
31
plugin/cache/cache_test.go
vendored
|
@ -208,6 +208,25 @@ func TestCache(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestCacheZeroTTL(t *testing.T) {
|
||||
c := &Cache{Zones: []string{"."}, pcap: defaultCap, ncap: defaultCap, pttl: maxTTL, nttl: maxTTL}
|
||||
c.pcache = cache.New(c.pcap)
|
||||
c.ncache = cache.New(c.ncap)
|
||||
c.Next = zeroTTLBackend()
|
||||
|
||||
req := new(dns.Msg)
|
||||
req.SetQuestion("example.org.", dns.TypeA)
|
||||
ctx := context.TODO()
|
||||
|
||||
c.ServeDNS(ctx, &test.ResponseWriter{}, req)
|
||||
if c.pcache.Len() != 0 {
|
||||
t.Errorf("Msg with 0 TTL should not have been cached")
|
||||
}
|
||||
if c.ncache.Len() != 0 {
|
||||
t.Errorf("Msg with 0 TTL should not have been cached")
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheResponse(b *testing.B) {
|
||||
c := &Cache{Zones: []string{"."}, pcap: defaultCap, ncap: defaultCap, pttl: maxTTL, nttl: maxTTL}
|
||||
c.pcache = cache.New(c.pcap)
|
||||
|
@ -249,3 +268,15 @@ func BackendHandler() plugin.Handler {
|
|||
return dns.RcodeSuccess, nil
|
||||
})
|
||||
}
|
||||
|
||||
func zeroTTLBackend() plugin.Handler {
|
||||
return plugin.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
m.Response, m.RecursionAvailable = true, true
|
||||
|
||||
m.Answer = []dns.RR{test.A("example.org. 0 IN A 127.0.0.53")}
|
||||
w.WriteMsg(m)
|
||||
return dns.RcodeSuccess, nil
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue