mirror of
https://github.com/coredns/coredns.git
synced 2026-06-15 13:40:11 -04:00
plugin/cache: allow cache TTLs above default 3600s (#8134)
* plugin/cache: allow cache TTLs above default 3600s This change allows the cache plugin to honor configured maximum TTL values above the default 3600s limit. Default behavior remains unchanged This PR fixes 7846 Signed-off-by: Yong Tang <yong.tang.github@outlook.com> * Keep MinimalTTL Signed-off-by: Yong Tang <yong.tang.github@outlook.com> --------- Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
3
plugin/cache/cache.go
vendored
3
plugin/cache/cache.go
vendored
@@ -221,14 +221,15 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
|
||||
// key returns empty string for anything we don't want to cache.
|
||||
hasKey, key := key(w.state.Name(), res, mt, w.do, w.cd)
|
||||
|
||||
msgTTL := dnsutil.MinimalTTL(res, mt)
|
||||
var duration time.Duration
|
||||
switch mt {
|
||||
case response.NameError, response.NoData:
|
||||
msgTTL := dnsutil.MinimalTTLWithMaximum(res, mt, w.nttl)
|
||||
duration = computeTTL(msgTTL, w.minnttl, w.nttl)
|
||||
case response.ServerError:
|
||||
duration = w.failttl
|
||||
default:
|
||||
msgTTL := dnsutil.MinimalTTLWithMaximum(res, mt, w.pttl)
|
||||
duration = computeTTL(msgTTL, w.minpttl, w.pttl)
|
||||
}
|
||||
|
||||
|
||||
21
plugin/cache/cache_test.go
vendored
21
plugin/cache/cache_test.go
vendored
@@ -362,6 +362,27 @@ func TestCacheZeroTTL(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheHonorsConfiguredPositiveMaxTTLAboveDefault(t *testing.T) {
|
||||
c := New()
|
||||
c.pttl = 2 * time.Hour
|
||||
c.minpttl = 0
|
||||
c.Next = ttlBackend(24 * 60 * 60)
|
||||
|
||||
req := new(dns.Msg)
|
||||
req.SetQuestion("example.org.", dns.TypeA)
|
||||
|
||||
rec := dnstest.NewRecorder(&test.ResponseWriter{})
|
||||
c.ServeDNS(context.TODO(), rec, req)
|
||||
|
||||
if rec.Msg == nil || len(rec.Msg.Answer) == 0 {
|
||||
t.Fatalf("expected answer, got %+v", rec.Msg)
|
||||
}
|
||||
|
||||
if got, want := rec.Msg.Answer[0].Header().Ttl, uint32(7200); got != want {
|
||||
t.Fatalf("expected TTL %d, got %d", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCacheServfailTTL0(t *testing.T) {
|
||||
c := New()
|
||||
c.minpttl = minTTL
|
||||
|
||||
Reference in New Issue
Block a user