| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | package cache | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2018-04-22 08:34:35 +01:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	"math" | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin" | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/metrics" | 
					
						
							| 
									
										
										
										
											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
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	zone := plugin.Zones(c.Zones).Matches(state.Name()) | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	now := c.now().UTC() | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	server := metrics.WithServer(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | 	i, found := c.get(now, state, server) | 
					
						
							|  |  |  | 	if i != nil && found { | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 		resp := i.toMsg(r, now) | 
					
						
							| 
									
										
										
										
											2017-06-26 07:44:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 { | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | 			ttl := i.ttl(now) | 
					
						
							| 
									
										
										
										
											2017-10-13 17:22:08 +02:00
										 |  |  | 			i.Freq.Update(c.duration, now) | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 			threshold := int(math.Ceil(float64(c.percentage) / 100 * float64(i.origTTL))) | 
					
						
							|  |  |  | 			if i.Freq.Hits() >= c.prefetch && ttl <= threshold { | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | 				cw := newPrefetchResponseWriter(server, state, c) | 
					
						
							|  |  |  | 				go func(w dns.ResponseWriter) { | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 					cachePrefetches.WithLabelValues(server).Inc() | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | 					plugin.NextOrFailure(c.Name(), c.Next, ctx, w, r) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 					// When prefetching we loose the item i, and with it the frequency | 
					
						
							|  |  |  | 					// that we've gathered sofar. See we copy the frequencies info back | 
					
						
							|  |  |  | 					// into the new item that was stored in the cache. | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 					if i1 := c.exists(state); i1 != nil { | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 						i1.Freq.Reset(now, i.Freq.Hits()) | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2018-06-19 20:50:08 +02:00
										 |  |  | 				}(cw) | 
					
						
							| 
									
										
										
										
											2017-06-13 12:39:10 -07:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 		return dns.RcodeSuccess, nil | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	crr := &ResponseWriter{ResponseWriter: w, Cache: c, state: state, server: server} | 
					
						
							| 
									
										
										
										
											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" } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | func (c *Cache) get(now time.Time, state request.Request, server string) (*item, bool) { | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	k := hash(state.Name(), state.QType(), state.Do()) | 
					
						
							| 
									
										
										
										
											2016-10-02 08:31:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | 	if i, ok := c.ncache.Get(k); ok && i.(*item).ttl(now) > 0 { | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 		cacheHits.WithLabelValues(server, Denial).Inc() | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | 		return i.(*item), true | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | 	if i, ok := c.pcache.Get(k); ok && i.(*item).ttl(now) > 0 { | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 		cacheHits.WithLabelValues(server, Success).Inc() | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | 		return i.(*item), true | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	cacheMisses.WithLabelValues(server).Inc() | 
					
						
							| 
									
										
										
										
											2018-06-26 15:11:17 +08:00
										 |  |  | 	return nil, false | 
					
						
							| 
									
										
										
										
											2016-04-19 11:13:24 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | func (c *Cache) exists(state request.Request) *item { | 
					
						
							|  |  |  | 	k := hash(state.Name(), state.QType(), state.Do()) | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 	if i, ok := c.ncache.Get(k); ok { | 
					
						
							|  |  |  | 		return i.(*item) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if i, ok := c.pcache.Get(k); ok { | 
					
						
							|  |  |  | 		return i.(*item) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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.", | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	}, []string{"server", "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.", | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	}, []string{"server", "type"}) | 
					
						
							| 
									
										
										
										
											2016-10-31 19:50:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	cacheMisses = 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:      "misses_total", | 
					
						
							|  |  |  | 		Help:      "The count of cache misses.", | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	}, []string{"server"}) | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	cachePrefetches = prometheus.NewCounterVec(prometheus.CounterOpts{ | 
					
						
							| 
									
										
										
										
											2018-01-17 08:35:22 +01:00
										 |  |  | 		Namespace: plugin.Namespace, | 
					
						
							|  |  |  | 		Subsystem: "cache", | 
					
						
							|  |  |  | 		Name:      "prefetch_total", | 
					
						
							|  |  |  | 		Help:      "The number of time the cache has prefetched a cached item.", | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	}, []string{"server"}) | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	cacheDrops = prometheus.NewCounterVec(prometheus.CounterOpts{ | 
					
						
							| 
									
										
										
										
											2018-03-25 17:11:10 +01:00
										 |  |  | 		Namespace: plugin.Namespace, | 
					
						
							|  |  |  | 		Subsystem: "cache", | 
					
						
							|  |  |  | 		Name:      "drops_total", | 
					
						
							|  |  |  | 		Help:      "The number responses that are not cached, because the reply is malformed.", | 
					
						
							| 
									
										
										
										
											2018-04-27 19:37:49 +01:00
										 |  |  | 	}, []string{"server"}) | 
					
						
							| 
									
										
										
										
											2016-04-21 21:46:58 +01:00
										 |  |  | ) |