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>
This commit is contained in:
parent
6938dac21d
commit
268781d355
3 changed files with 61 additions and 19 deletions
21
plugin/cache/cache.go
vendored
21
plugin/cache/cache.go
vendored
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue