| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 	"sync"
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2017-02-21 22:51:47 -08:00
										 |  |  | 	"github.com/coredns/coredns/request"
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/miekg/dns"
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 	"github.com/prometheus/client_golang/prometheus"
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	"golang.org/x/net/context"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // ServeDNS implements the plugin.Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | func (c *Cache) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	state := request.Request{W: w, Req: r}
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	qname := state.Name()
 | 
					
						
							|  |  |  | 	qtype := state.QType()
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	zone := plugin.Zones(c.Zones).Matches(qname)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	if zone == "" {
 | 
					
						
							| 
									
										
										
										
											2017-09-20 11:36:41 +01:00
										 |  |  | 		return plugin.NextOrFailure(c.Name(), c.Next, ctx, w, r)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-26 17:57:22 +00:00
										 |  |  | 	do := state.Do() // TODO(): might need more from OPT record? Like the actual bufsize?
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	now := time.Now().UTC()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	i, ttl := c.get(now, qname, qtype, do)
 | 
					
						
							|  |  |  | 	if i != nil && ttl > 0 {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		resp := i.toMsg(r)
 | 
					
						
							| 
									
										
										
										
											2017-06-26 07:44:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		state.SizeAndDo(resp)
 | 
					
						
							| 
									
										
										
										
											2017-01-22 20:32:08 +00:00
										 |  |  | 		resp, _ = state.Scrub(resp)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		w.WriteMsg(resp)
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-13 17:22:08 +02:00
										 |  |  | 		if c.prefetch > 0 {
 | 
					
						
							|  |  |  | 			i.Freq.Update(c.duration, now)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		pct := 100
 | 
					
						
							|  |  |  | 		if i.origTTL != 0 { // you'll never know
 | 
					
						
							|  |  |  | 			pct = int(float64(ttl) / float64(i.origTTL) * 100)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if c.prefetch > 0 && i.Freq.Hits() > c.prefetch && pct < c.percentage {
 | 
					
						
							|  |  |  | 			// When prefetching we loose the item i, and with it the frequency
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:06:06 -07:00
										 |  |  | 			// that we've gathered sofar. See we copy the frequencies info back
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 			// into the new item that was stored in the cache.
 | 
					
						
							|  |  |  | 			prr := &ResponseWriter{ResponseWriter: w, Cache: c, prefetch: true}
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 			plugin.NextOrFailure(c.Name(), c.Next, ctx, prr, r)
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if i1, _ := c.get(now, qname, qtype, do); i1 != nil {
 | 
					
						
							|  |  |  | 				i1.Freq.Reset(now, i.Freq.Hits())
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return dns.RcodeSuccess, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	crr := &ResponseWriter{ResponseWriter: w, Cache: c}
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	return plugin.NextOrFailure(c.Name(), c.Next, ctx, crr, r)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-27 11:48:37 +00:00
										 |  |  | // Name implements the Handler interface.
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | func (c *Cache) Name() string { return "cache" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | func (c *Cache) get(now time.Time, qname string, qtype uint16, do bool) (*item, int) {
 | 
					
						
							|  |  |  | 	k := hash(qname, qtype, do)
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if i, ok := c.ncache.Get(k); ok {
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 		cacheHits.WithLabelValues(Denial).Inc()
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 		return i.(*item), i.(*item).ttl(now)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	if i, ok := c.pcache.Get(k); ok {
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 		cacheHits.WithLabelValues(Success).Inc()
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 		return i.(*item), i.(*item).ttl(now)
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 	cacheMisses.Inc()
 | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 	return nil, 0
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	cacheSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		Namespace: plugin.Namespace,
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 		Subsystem: "cache",
 | 
					
						
							| 
									
										
										
										
											2016-10-30 10:06:57 +01:00
										 |  |  | 		Name:      "size",
 | 
					
						
							|  |  |  | 		Help:      "The number of elements in the cache.",
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	}, []string{"type"})
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	cacheCapacity = prometheus.NewGaugeVec(prometheus.GaugeOpts{
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		Namespace: plugin.Namespace,
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 		Subsystem: "cache",
 | 
					
						
							| 
									
										
										
										
											2016-10-30 10:06:57 +01:00
										 |  |  | 		Name:      "capacity",
 | 
					
						
							|  |  |  | 		Help:      "The cache's capacity.",
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	}, []string{"type"})
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	cacheHits = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		Namespace: plugin.Namespace,
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 		Subsystem: "cache",
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 		Name:      "hits_total",
 | 
					
						
							|  |  |  | 		Help:      "The count of cache hits.",
 | 
					
						
							|  |  |  | 	}, []string{"type"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		Namespace: plugin.Namespace,
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | 		Subsystem: "cache",
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 		Name:      "misses_total",
 | 
					
						
							|  |  |  | 		Help:      "The count of cache misses.",
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-27 15:48:14 +00:00
										 |  |  | var once sync.Once
 |