diff options
author | 2020-10-15 16:47:07 +0200 | |
---|---|---|
committer | 2020-10-15 07:47:07 -0700 | |
commit | 268781d3553cca94f5e5ecbd248a537fbdf6dae8 (patch) | |
tree | 21c5e80cbdceca2e85bcc282ca77add3df8fc634 /plugin/cache/cache.go | |
parent | 6938dac21d9a79bc34f4347a52fda4d1bf24b259 (diff) | |
download | coredns-268781d3553cca94f5e5ecbd248a537fbdf6dae8.tar.gz coredns-268781d3553cca94f5e5ecbd248a537fbdf6dae8.tar.zst coredns-268781d3553cca94f5e5ecbd248a537fbdf6dae8.zip |
cache: do the msg copy right (#4207)
Not sure why this is proving so difficult.. pointers are hard? [Was
tempted to rollback all tweaks here, but the original issue we're fixing
it too important to not have a proper fix].
But we need to make a copy of the message at the earliest point in the
handler because we are changing it (adding an opt rr). If we do this on
the original message (which is a pointer) we change it (obvs). When
undoing those changes we do work on a copy.
Re: testing. There isn't a explicit test for this, so I've added on to
the top-level test/ directory, which indeed makes the issue visible:
master:
~~~
go test -v -run=TestLookupCacheWithoutEdns
=== RUN TestLookupCacheWithoutEdns
cache_test.go:154: Expected no OPT RR, but got:
;; OPT PSEUDOSECTION:
; EDNS: version 0; flags: do; udp: 2048
--- FAIL: TestLookupCacheWithoutEdns (0.01s)
FAIL
~~~
This branch:
~~~
% go test -v -run=TestLookupCacheWithoutEdns
=== RUN TestLookupCacheWithoutEdns
--- PASS: TestLookupCacheWithoutEdns (0.01s)
PASS
ok github.com/coredns/coredns/test 0.109s
~~~
Signed-off-by: Miek Gieben <miek@miek.nl>
Diffstat (limited to 'plugin/cache/cache.go')
-rw-r--r-- | plugin/cache/cache.go | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/plugin/cache/cache.go b/plugin/cache/cache.go index 8dae9a42b..2a56500a3 100644 --- a/plugin/cache/cache.go +++ b/plugin/cache/cache.go @@ -143,15 +143,12 @@ func (w *ResponseWriter) RemoteAddr() net.Addr { // WriteMsg implements the dns.ResponseWriter interface. func (w *ResponseWriter) WriteMsg(res *dns.Msg) error { - // res needs to be copied otherwise we will be modifying the underlaying arrays which are now cached. - resc := res.Copy() - - mt, _ := response.Typify(resc, w.now().UTC()) + mt, _ := response.Typify(res, w.now().UTC()) // key returns empty string for anything we don't want to cache. - hasKey, key := key(w.state.Name(), resc, mt) + hasKey, key := key(w.state.Name(), res, mt) - msgTTL := dnsutil.MinimalTTL(resc, mt) + msgTTL := dnsutil.MinimalTTL(res, mt) var duration time.Duration if mt == response.NameError || mt == response.NoData { duration = computeTTL(msgTTL, w.minnttl, w.nttl) @@ -163,8 +160,8 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error { } if hasKey && duration > 0 { - if w.state.Match(resc) { - w.set(resc, key, mt, duration) + if w.state.Match(res) { + w.set(res, key, mt, duration) cacheSize.WithLabelValues(w.server, Success).Set(float64(w.pcache.Len())) cacheSize.WithLabelValues(w.server, Denial).Set(float64(w.ncache.Len())) } else { @@ -180,11 +177,11 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error { // Apply capped TTL to this reply to avoid jarring TTL experience 1799 -> 8 (e.g.) // We also may need to filter out DNSSEC records, see toMsg() for similar code. ttl := uint32(duration.Seconds()) - resc.Answer = filterRRSlice(resc.Answer, ttl, w.do, false) - resc.Ns = filterRRSlice(resc.Ns, ttl, w.do, false) - resc.Extra = filterRRSlice(resc.Extra, ttl, w.do, false) + res.Answer = filterRRSlice(res.Answer, ttl, w.do, false) + res.Ns = filterRRSlice(res.Ns, ttl, w.do, false) + res.Extra = filterRRSlice(res.Extra, ttl, w.do, false) - return w.ResponseWriter.WriteMsg(resc) + return w.ResponseWriter.WriteMsg(res) } func (w *ResponseWriter) set(m *dns.Msg, key uint64, mt response.Type, duration time.Duration) { |