-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8scache.go
101 lines (88 loc) · 2.51 KB
/
k8scache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package cache
import (
"time"
"github.com/coredns/coredns/plugin/pkg/cache"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
)
type Cache struct {
*CacheBackend
// Late positive cache. CacheBackend.pcache is the early cache
latepcache *cache.Cache
extrattl time.Duration
k8sAPI *k8sAPI
}
func New() *Cache {
cb := NewBackend()
return &Cache{
CacheBackend: cb,
latepcache: cache.New(defaultCap),
k8sAPI: &k8sAPI{},
}
}
// Copy item to c.latepcache if the conditions are right
func (c *Cache) copyToLate(key uint64, i *item, now time.Time) {
if i.Rcode == dns.RcodeSuccess {
ii, exists := c.latepcache.Get(key)
add := false
if exists {
li := ii.(*item)
if li.ttl(now) <= 0 {
add = true
}
} else {
add = true
}
if add {
newi := *i
newi.origTTL += uint32(c.extrattl.Seconds())
c.latepcache.Add(key, &newi)
}
}
}
// Get cache item for c.ncache or c.pcache (early cache). Only ncache item can be stale
func (c *Cache) getEarly(now time.Time, state request.Request, server string) *item {
k := hash(state.Name(), state.QType(), state.Do(), state.Req.CheckingDisabled)
if i, ok := c.ncache.Get(k); ok {
itm := i.(*item)
ttl := itm.ttl(now)
if itm.matches(state) && (ttl > 0 || (c.staleUpTo > 0 && -ttl < int(c.staleUpTo.Seconds()))) {
cacheHits.WithLabelValues(server, Denial, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return i.(*item)
}
}
if i, ok := c.pcache.Get(k); ok {
itm := i.(*item)
ttl := itm.ttl(now)
if itm.matches(state) && ttl > 0 {
cacheHits.WithLabelValues(server, Success, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return i.(*item)
}
}
cacheMisses.WithLabelValues(server, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return nil
}
func (c *Cache) getLate(now time.Time, state request.Request, server string) *item {
k := hash(state.Name(), state.QType(), state.Do(), state.Req.CheckingDisabled)
cacheRequests.WithLabelValues(server, c.zonesMetricLabel, c.viewMetricLabel).Inc()
if i, ok := c.latepcache.Get(k); ok {
itm := i.(*item)
ttl := itm.ttl(now)
staleupto := c.staleUpTo - c.extrattl
if itm.matches(state) && (ttl > 0 || (staleupto > 0 && -ttl < int(staleupto.Seconds()))) {
cacheHits.WithLabelValues(server, Success, c.zonesMetricLabel, c.viewMetricLabel).Inc()
return i.(*item)
}
}
return nil
}
func (c *Cache) NeedEarlyRefresh(state request.Request) bool {
earlyips := c.k8sAPI.getEarlyRefreshIPs()
me := state.IP()
for _, ip := range earlyips {
if ip == me {
return true
}
}
return false
}