aboutsummaryrefslogtreecommitdiff
path: root/plugin/cache
diff options
context:
space:
mode:
Diffstat (limited to 'plugin/cache')
-rw-r--r--plugin/cache/cache.go8
-rw-r--r--plugin/cache/item.go56
-rw-r--r--plugin/cache/minttl_test.go72
3 files changed, 4 insertions, 132 deletions
diff --git a/plugin/cache/cache.go b/plugin/cache/cache.go
index ed39fee86..c46267658 100644
--- a/plugin/cache/cache.go
+++ b/plugin/cache/cache.go
@@ -9,6 +9,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/cache"
+ "github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/plugin/pkg/response"
"github.com/coredns/coredns/request"
@@ -158,7 +159,7 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
duration = w.nttl
}
- msgTTL := minMsgTTL(res, mt)
+ msgTTL := dnsutil.MinimalTTL(res, mt)
if msgTTL < duration {
duration = msgTTL
}
@@ -226,9 +227,8 @@ func (w *ResponseWriter) Write(buf []byte) (int, error) {
}
const (
- maxTTL = 1 * time.Hour
- maxNTTL = 30 * time.Minute
- failSafeTTL = 5 * time.Second
+ maxTTL = dnsutil.MaximumDefaulTTL
+ maxNTTL = dnsutil.MaximumDefaulTTL / 2
defaultCap = 10000 // default capacity of the cache.
diff --git a/plugin/cache/item.go b/plugin/cache/item.go
index 5761cdf87..f4858c3b1 100644
--- a/plugin/cache/item.go
+++ b/plugin/cache/item.go
@@ -4,7 +4,6 @@ import (
"time"
"github.com/coredns/coredns/plugin/cache/freq"
- "github.com/coredns/coredns/plugin/pkg/response"
"github.com/miekg/dns"
)
@@ -87,58 +86,3 @@ func (i *item) ttl(now time.Time) int {
ttl := int(i.origTTL) - int(now.UTC().Sub(i.stored).Seconds())
return ttl
}
-
-func minMsgTTL(m *dns.Msg, mt response.Type) time.Duration {
- if mt != response.NoError && mt != response.NameError && mt != response.NoData {
- return 0
- }
-
- // No data to examine, return a short ttl as a fail safe.
- if len(m.Answer)+len(m.Ns)+len(m.Extra) == 0 {
- return failSafeTTL
- }
-
- minTTL := maxTTL
- for _, r := range m.Answer {
- switch mt {
- case response.NameError, response.NoData:
- if r.Header().Rrtype == dns.TypeSOA {
- minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
- }
- case response.NoError, response.Delegation:
- if r.Header().Ttl < uint32(minTTL.Seconds()) {
- minTTL = time.Duration(r.Header().Ttl) * time.Second
- }
- }
- }
- for _, r := range m.Ns {
- switch mt {
- case response.NameError, response.NoData:
- if r.Header().Rrtype == dns.TypeSOA {
- minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
- }
- case response.NoError, response.Delegation:
- if r.Header().Ttl < uint32(minTTL.Seconds()) {
- minTTL = time.Duration(r.Header().Ttl) * time.Second
- }
- }
- }
-
- for _, r := range m.Extra {
- if r.Header().Rrtype == dns.TypeOPT {
- // OPT records use TTL field for extended rcode and flags
- continue
- }
- switch mt {
- case response.NameError, response.NoData:
- if r.Header().Rrtype == dns.TypeSOA {
- minTTL = time.Duration(r.(*dns.SOA).Minttl) * time.Second
- }
- case response.NoError, response.Delegation:
- if r.Header().Ttl < uint32(minTTL.Seconds()) {
- minTTL = time.Duration(r.Header().Ttl) * time.Second
- }
- }
- }
- return minTTL
-}
diff --git a/plugin/cache/minttl_test.go b/plugin/cache/minttl_test.go
deleted file mode 100644
index 376c638a1..000000000
--- a/plugin/cache/minttl_test.go
+++ /dev/null
@@ -1,72 +0,0 @@
-package cache
-
-import (
- "testing"
- "time"
-
- "github.com/coredns/coredns/plugin/pkg/response"
- "github.com/coredns/coredns/plugin/test"
-
- "github.com/miekg/dns"
-)
-
-// See https://github.com/kubernetes/dns/issues/121, add some specific tests for those use cases.
-
-func TestMinMsgTTL(t *testing.T) {
- m := new(dns.Msg)
- m.SetQuestion("z.alm.im.", dns.TypeA)
- m.Ns = []dns.RR{
- test.SOA("alm.im. 1800 IN SOA ivan.ns.cloudflare.com. dns.cloudflare.com. 2025042470 10000 2400 604800 3600"),
- }
-
- utc := time.Now().UTC()
-
- mt, _ := response.Typify(m, utc)
- if mt != response.NoData {
- t.Fatalf("Expected type to be response.NoData, got %s", mt)
- }
- dur := minMsgTTL(m, mt) // minTTL on msg is 3600 (neg. ttl on SOA)
- if dur != time.Duration(3600*time.Second) {
- t.Fatalf("Expected minttl duration to be %d, got %d", 3600, dur)
- }
-
- m.Rcode = dns.RcodeNameError
- mt, _ = response.Typify(m, utc)
- if mt != response.NameError {
- t.Fatalf("Expected type to be response.NameError, got %s", mt)
- }
- dur = minMsgTTL(m, mt) // minTTL on msg is 3600 (neg. ttl on SOA)
- if dur != time.Duration(3600*time.Second) {
- t.Fatalf("Expected minttl duration to be %d, got %d", 3600, dur)
- }
-}
-
-func BenchmarkMinMsgTTL(b *testing.B) {
- m := new(dns.Msg)
- m.SetQuestion("example.org.", dns.TypeA)
- m.Ns = []dns.RR{
- test.A("a.example.org. 1800 IN A 127.0.0.53"),
- test.A("b.example.org. 1900 IN A 127.0.0.53"),
- test.A("c.example.org. 1600 IN A 127.0.0.53"),
- test.A("d.example.org. 1100 IN A 127.0.0.53"),
- test.A("e.example.org. 1000 IN A 127.0.0.53"),
- }
- m.Extra = []dns.RR{
- test.A("a.example.org. 1800 IN A 127.0.0.53"),
- test.A("b.example.org. 1600 IN A 127.0.0.53"),
- test.A("c.example.org. 1400 IN A 127.0.0.53"),
- test.A("d.example.org. 1200 IN A 127.0.0.53"),
- test.A("e.example.org. 1100 IN A 127.0.0.53"),
- }
-
- utc := time.Now().UTC()
- mt, _ := response.Typify(m, utc)
-
- b.ResetTimer()
- for i := 0; i < b.N; i++ {
- dur := minMsgTTL(m, mt)
- if dur != 1000*time.Second {
- b.Fatalf("Wrong minMsgTTL %d, expected %d", dur, 1000*time.Second)
- }
- }
-}