aboutsummaryrefslogtreecommitdiff
path: root/plugin/cache/cache.go
diff options
context:
space:
mode:
authorGravatar Aaron Riekenberg <aaron.riekenberg@gmail.com> 2018-09-03 14:26:02 -0500
committerGravatar Tobias Schmidt <tobidt@gmail.com> 2018-09-03 21:26:02 +0200
commitb42eae7a04b8f789d7a20c0664895cd8e4638a22 (patch)
treece3c4fe568fda2368e9c103451d6c4364ac1e349 /plugin/cache/cache.go
parent4c6c9d4b2700c3e4606d4b98bde64e7c1ed0c231 (diff)
downloadcoredns-b42eae7a04b8f789d7a20c0664895cd8e4638a22.tar.gz
coredns-b42eae7a04b8f789d7a20c0664895cd8e4638a22.tar.zst
coredns-b42eae7a04b8f789d7a20c0664895cd8e4638a22.zip
Add MINTTL parameter to cache configuration. (#2055)
* Add success min TTL parameter to cache. * Add MINTTL to README. * Update README. * Add MINTTL to negative cache. * Remove unnecessary variable name. * Address review comments. * Configure cache in TestCacheZeroTTL to have 0 min ttl.
Diffstat (limited to 'plugin/cache/cache.go')
-rw-r--r--plugin/cache/cache.go41
1 files changed, 28 insertions, 13 deletions
diff --git a/plugin/cache/cache.go b/plugin/cache/cache.go
index 654841b23..9ff43dac2 100644
--- a/plugin/cache/cache.go
+++ b/plugin/cache/cache.go
@@ -22,13 +22,15 @@ type Cache struct {
Next plugin.Handler
Zones []string
- ncache *cache.Cache
- ncap int
- nttl time.Duration
+ ncache *cache.Cache
+ ncap int
+ nttl time.Duration
+ minnttl time.Duration
- pcache *cache.Cache
- pcap int
- pttl time.Duration
+ pcache *cache.Cache
+ pcap int
+ pttl time.Duration
+ minpttl time.Duration
// Prefetch.
prefetch int
@@ -47,9 +49,11 @@ func New() *Cache {
pcap: defaultCap,
pcache: cache.New(defaultCap),
pttl: maxTTL,
+ minpttl: minTTL,
ncap: defaultCap,
ncache: cache.New(defaultCap),
nttl: maxNTTL,
+ minnttl: minNTTL,
prefetch: 0,
duration: 1 * time.Minute,
percentage: 10,
@@ -100,6 +104,17 @@ func hash(qname string, qtype uint16, do bool) uint64 {
return h.Sum64()
}
+func computeTTL(msgTTL, minTTL, maxTTL time.Duration) time.Duration {
+ ttl := msgTTL
+ if ttl < minTTL {
+ ttl = minTTL
+ }
+ if ttl > maxTTL {
+ ttl = maxTTL
+ }
+ return ttl
+}
+
// ResponseWriter is a response writer that caches the reply message.
type ResponseWriter struct {
dns.ResponseWriter
@@ -154,14 +169,12 @@ func (w *ResponseWriter) WriteMsg(res *dns.Msg) error {
// key returns empty string for anything we don't want to cache.
hasKey, key := key(res, mt, do)
- duration := w.pttl
- if mt == response.NameError || mt == response.NoData {
- duration = w.nttl
- }
-
msgTTL := dnsutil.MinimalTTL(res, mt)
- if msgTTL < duration {
- duration = msgTTL
+ var duration time.Duration
+ if mt == response.NameError || mt == response.NoData {
+ duration = computeTTL(msgTTL, w.minnttl, w.nttl)
+ } else {
+ duration = computeTTL(msgTTL, w.minpttl, w.pttl)
}
if hasKey && duration > 0 {
@@ -226,7 +239,9 @@ func (w *ResponseWriter) Write(buf []byte) (int, error) {
const (
maxTTL = dnsutil.MaximumDefaulTTL
+ minTTL = dnsutil.MinimalDefaultTTL
maxNTTL = dnsutil.MaximumDefaulTTL / 2
+ minNTTL = dnsutil.MinimalDefaultTTL
defaultCap = 10000 // default capacity of the cache.