mirror of
				https://github.com/coredns/coredns.git
				synced 2025-10-31 10:13:14 -04:00 
			
		
		
		
	* Improve plugin/cache metrics * Add coredns_cache_prefetch_total metric to track number of prefetches. * Remove unnecessary Cache.get() call which would incorrectly increment cache counters. * Initialize all counters and gauges at zero. * Allow prefetching of a single request per ttl The original implementation didn't allow prefetching queries which are only requested once during the duration of a TTL. The minimum amount of queries which had to be seen was therefore capped at 2. This change also implements a real prefetch test. The existing test was a noop and always passed regardless of any prefetch implementation. * Fix prefetching for items with a short TTL The default prefetch threshold (percentage) is 10% of the lifetime of a cache item. With the previous implementation, this disabled prefetching for all items with a TTL < 10s (the resulting percentage would be 0, at which point a cached item is already discarded). This change uses a time based threshold calculation and ensures that a prefetch is triggered at a TTL of 1 at the latest. * Fix wrong duration reporting of cached responses The logging and metrics plugins (among others) included the duration of a cache prefetch in the request latency of client request. This change fixes this wrong reporting and executes the prefetch request in a goroutine in the background.
		
			
				
	
	
		
			113 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			2.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package cache
 | |
| 
 | |
| import (
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/coredns/coredns/plugin/cache/freq"
 | |
| 	"github.com/coredns/coredns/plugin/pkg/response"
 | |
| 	"github.com/miekg/dns"
 | |
| )
 | |
| 
 | |
| type item struct {
 | |
| 	Rcode              int
 | |
| 	Authoritative      bool
 | |
| 	AuthenticatedData  bool
 | |
| 	RecursionAvailable bool
 | |
| 	Answer             []dns.RR
 | |
| 	Ns                 []dns.RR
 | |
| 	Extra              []dns.RR
 | |
| 
 | |
| 	origTTL uint32
 | |
| 	stored  time.Time
 | |
| 
 | |
| 	*freq.Freq
 | |
| }
 | |
| 
 | |
| func newItem(m *dns.Msg, now time.Time, d time.Duration) *item {
 | |
| 	i := new(item)
 | |
| 	i.Rcode = m.Rcode
 | |
| 	i.Authoritative = m.Authoritative
 | |
| 	i.AuthenticatedData = m.AuthenticatedData
 | |
| 	i.RecursionAvailable = m.RecursionAvailable
 | |
| 	i.Answer = m.Answer
 | |
| 	i.Ns = m.Ns
 | |
| 	i.Extra = make([]dns.RR, len(m.Extra))
 | |
| 	// Don't copy OPT record as these are hop-by-hop.
 | |
| 	j := 0
 | |
| 	for _, e := range m.Extra {
 | |
| 		if e.Header().Rrtype == dns.TypeOPT {
 | |
| 			continue
 | |
| 		}
 | |
| 		i.Extra[j] = e
 | |
| 		j++
 | |
| 	}
 | |
| 	i.Extra = i.Extra[:j]
 | |
| 
 | |
| 	i.origTTL = uint32(d.Seconds())
 | |
| 	i.stored = now.UTC()
 | |
| 
 | |
| 	i.Freq = new(freq.Freq)
 | |
| 
 | |
| 	return i
 | |
| }
 | |
| 
 | |
| // toMsg turns i into a message, it tailors the reply to m.
 | |
| // The Authoritative bit is always set to 0, because the answer is from the cache.
 | |
| func (i *item) toMsg(m *dns.Msg, now time.Time) *dns.Msg {
 | |
| 	m1 := new(dns.Msg)
 | |
| 	m1.SetReply(m)
 | |
| 
 | |
| 	m1.Authoritative = false
 | |
| 	m1.AuthenticatedData = i.AuthenticatedData
 | |
| 	m1.RecursionAvailable = i.RecursionAvailable
 | |
| 	m1.Rcode = i.Rcode
 | |
| 	m1.Compress = true
 | |
| 
 | |
| 	m1.Answer = make([]dns.RR, len(i.Answer))
 | |
| 	m1.Ns = make([]dns.RR, len(i.Ns))
 | |
| 	m1.Extra = make([]dns.RR, len(i.Extra))
 | |
| 
 | |
| 	ttl := uint32(i.ttl(now))
 | |
| 	for j, r := range i.Answer {
 | |
| 		m1.Answer[j] = dns.Copy(r)
 | |
| 		m1.Answer[j].Header().Ttl = ttl
 | |
| 	}
 | |
| 	for j, r := range i.Ns {
 | |
| 		m1.Ns[j] = dns.Copy(r)
 | |
| 		m1.Ns[j].Header().Ttl = ttl
 | |
| 	}
 | |
| 	for j, r := range i.Extra {
 | |
| 		m1.Extra[j] = dns.Copy(r)
 | |
| 		if m1.Extra[j].Header().Rrtype != dns.TypeOPT {
 | |
| 			m1.Extra[j].Header().Ttl = ttl
 | |
| 		}
 | |
| 	}
 | |
| 	return m1
 | |
| }
 | |
| 
 | |
| 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
 | |
| 	}
 | |
| 
 | |
| 	minTTL := maxTTL
 | |
| 	for _, r := range append(m.Answer, m.Ns...) {
 | |
| 		switch mt {
 | |
| 		case response.NameError, response.NoData:
 | |
| 			if r.Header().Rrtype == dns.TypeSOA {
 | |
| 				return 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
 | |
| }
 |