| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"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.
 | 
					
						
							|  |  |  | 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)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	do := state.Do() // might need more from OPT record?
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	if i, ok := c.get(qname, qtype, do); ok {
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 		cacheHitCount.WithLabelValues(zone).Inc()
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return dns.RcodeSuccess, nil
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 	cacheMissCount.WithLabelValues(zone).Inc()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	crr := NewCachingResponseWriter(w, c.cache, c.cap)
 | 
					
						
							|  |  |  | 	return c.Next.ServeDNS(ctx, crr, r)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | func (c Cache) get(qname string, qtype uint16, do bool) (*item, bool) {
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	nxdomain := nameErrorKey(qname, do)
 | 
					
						
							|  |  |  | 	if i, ok := c.cache.Get(nxdomain); ok {
 | 
					
						
							|  |  |  | 		return i.(*item), true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-26 17:57:11 +01:00
										 |  |  | 	// TODO(miek): delegation was added double check
 | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	successOrNoData := successKey(qname, qtype, do)
 | 
					
						
							|  |  |  | 	if i, ok := c.cache.Get(successOrNoData); ok {
 | 
					
						
							|  |  |  | 		return i.(*item), true
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil, false
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | var (
 | 
					
						
							|  |  |  | 	cacheHitCount = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "hit_count_total",
 | 
					
						
							|  |  |  | 		Help:      "Counter of DNS requests that were found in the cache.",
 | 
					
						
							|  |  |  | 	}, []string{"zone"})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cacheMissCount = prometheus.NewCounterVec(prometheus.CounterOpts{
 | 
					
						
							|  |  |  | 		Namespace: middleware.Namespace,
 | 
					
						
							|  |  |  | 		Subsystem: subsystem,
 | 
					
						
							|  |  |  | 		Name:      "miss_count_total",
 | 
					
						
							|  |  |  | 		Help:      "Counter of DNS requests that were not found in the cache.",
 | 
					
						
							|  |  |  | 	}, []string{"zone"})
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const subsystem = "cache"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() {
 | 
					
						
							|  |  |  | 	prometheus.MustRegister(cacheHitCount)
 | 
					
						
							|  |  |  | 	prometheus.MustRegister(cacheMissCount)
 | 
					
						
							|  |  |  | }
 |