| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	"github.com/miekg/coredns/middleware"
 | 
					
						
							| 
									
										
										
										
											2016-09-07 11:10:16 +01:00
										 |  |  | 	"github.com/miekg/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"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServeDNS implements the middleware.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()
 | 
					
						
							|  |  |  | 	zone := middleware.Zones(c.Zones).Matches(qname)
 | 
					
						
							|  |  |  | 	if zone == "" {
 | 
					
						
							|  |  |  | 		return c.Next.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 	if i, ok, expired := c.get(qname, qtype, do); ok && !expired {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		resp := i.toMsg(r)
 | 
					
						
							|  |  |  | 		state.SizeAndDo(resp)
 | 
					
						
							|  |  |  | 		w.WriteMsg(resp)
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return dns.RcodeSuccess, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	crr := &ResponseWriter{w, c}
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	return c.Next.ServeDNS(ctx, crr, r)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | func (c *Cache) get(qname string, qtype uint16, do bool) (*item, bool, bool) {
 | 
					
						
							|  |  |  | 	k := rawKey(qname, qtype, do)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if i, ok := c.ncache.Get(k); ok {
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 		cacheHits.WithLabelValues(Denial).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		return i.(*item), ok, i.(*item).expired(time.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()
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 		return i.(*item), ok, i.(*item).expired(time.Now())
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 	cacheMisses.Inc()
 | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	return nil, false, false
 | 
					
						
							| 
									
										
										
										
											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{
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							| 
									
										
										
										
											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{
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							| 
									
										
										
										
											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{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "hits_total",
 | 
					
						
							|  |  |  | 		Help:      "The count of cache hits.",
 | 
					
						
							|  |  |  | 	}, []string{"type"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cacheMisses = prometheus.NewCounter(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "misses_total",
 | 
					
						
							|  |  |  | 		Help:      "The count of cache misses.",
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const subsystem = "cache"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							| 
									
										
										
										
											2016-10-26 10:01:52 +01:00
										 |  |  | 	prometheus.MustRegister(cacheSize)
 | 
					
						
							|  |  |  | 	prometheus.MustRegister(cacheCapacity)
 | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 	prometheus.MustRegister(cacheHits)
 | 
					
						
							|  |  |  | 	prometheus.MustRegister(cacheMisses)
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | }
 |