-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
caching_resolver.go
280 lines (216 loc) · 8.21 KB
/
caching_resolver.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package resolver
import (
"fmt"
"time"
"github.com/hako/durafmt"
"github.com/0xERR0R/blocky/cache/expirationcache"
"github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/evt"
"github.com/0xERR0R/blocky/model"
"github.com/0xERR0R/blocky/redis"
"github.com/0xERR0R/blocky/util"
"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)
const defaultCachingCleanUpInterval = 5 * time.Second
// CachingResolver caches answers from dns queries with their TTL time,
// to avoid external resolver calls for recurrent queries
type CachingResolver struct {
NextResolver
minCacheTimeSec, maxCacheTimeSec int
cacheTimeNegative time.Duration
resultCache expirationcache.ExpiringCache
prefetchExpires time.Duration
prefetchThreshold int
prefetchingNameCache expirationcache.ExpiringCache
redisClient *redis.Client
redisEnabled bool
}
// cacheValue includes query answer and prefetch flag
type cacheValue struct {
answer []dns.RR
prefetch bool
}
// NewCachingResolver creates a new resolver instance
func NewCachingResolver(cfg config.CachingConfig, redis *redis.Client) ChainedResolver {
c := &CachingResolver{
minCacheTimeSec: int(time.Duration(cfg.MinCachingTime).Seconds()),
maxCacheTimeSec: int(time.Duration(cfg.MaxCachingTime).Seconds()),
cacheTimeNegative: time.Duration(cfg.CacheTimeNegative),
redisClient: redis,
redisEnabled: (redis != nil),
}
configureCaches(c, &cfg)
if c.redisEnabled {
setupRedisCacheSubscriber(c)
c.redisClient.GetRedisCache()
}
return c
}
func configureCaches(c *CachingResolver, cfg *config.CachingConfig) {
cleanupOption := expirationcache.WithCleanUpInterval(defaultCachingCleanUpInterval)
maxSizeOption := expirationcache.WithMaxSize(uint(cfg.MaxItemsCount))
if cfg.Prefetching {
c.prefetchExpires = time.Duration(cfg.PrefetchExpires)
c.prefetchThreshold = cfg.PrefetchThreshold
c.prefetchingNameCache = expirationcache.NewCache(expirationcache.WithCleanUpInterval(time.Minute),
expirationcache.WithMaxSize(uint(cfg.PrefetchMaxItemsCount)))
c.resultCache = expirationcache.NewCache(cleanupOption, maxSizeOption,
expirationcache.WithOnExpiredFn(c.onExpired))
} else {
c.resultCache = expirationcache.NewCache(cleanupOption, maxSizeOption)
}
}
func setupRedisCacheSubscriber(c *CachingResolver) {
logger := logger("caching_resolver")
go func() {
for rc := range c.redisClient.CacheChannel {
if rc != nil {
logger.Debug("Received key from redis: ", rc.Key)
c.putInCache(rc.Key, rc.Response, false, false)
}
}
}()
}
// check if domain was queried > threshold in the time window
func (r *CachingResolver) isPrefetchingDomain(cacheKey string) bool {
cnt, _ := r.prefetchingNameCache.Get(cacheKey)
return cnt != nil && cnt.(int) > r.prefetchThreshold
}
func (r *CachingResolver) onExpired(cacheKey string) (val interface{}, ttl time.Duration) {
qType, domainName := util.ExtractCacheKey(cacheKey)
logger := logger("caching_resolver")
if r.isPrefetchingDomain(cacheKey) {
logger.Debugf("prefetching '%s' (%s)", util.Obfuscate(domainName), qType.String())
req := newRequest(fmt.Sprintf("%s.", domainName), qType, logger)
response, err := r.next.Resolve(req)
if err == nil {
if response.Res.Rcode == dns.RcodeSuccess {
evt.Bus().Publish(evt.CachingDomainPrefetched, domainName)
return cacheValue{response.Res.Answer, true}, r.adjustTTLs(response.Res.Answer)
}
} else {
util.LogOnError(fmt.Sprintf("can't prefetch '%s' ", domainName), err)
}
}
return nil, 0
}
// Configuration returns a current resolver configuration
func (r *CachingResolver) Configuration() (result []string) {
if r.maxCacheTimeSec < 0 {
return configDisabled
}
result = append(result, fmt.Sprintf("minCacheTimeInSec = %d", r.minCacheTimeSec))
result = append(result, fmt.Sprintf("maxCacheTimeSec = %d", r.maxCacheTimeSec))
result = append(result, fmt.Sprintf("cacheTimeNegative = %s", durafmt.Parse(r.cacheTimeNegative)))
result = append(result, fmt.Sprintf("prefetching = %t", r.prefetchingNameCache != nil))
if r.prefetchingNameCache != nil {
result = append(result, fmt.Sprintf("prefetchExpires = %s", durafmt.Parse(r.prefetchExpires)))
result = append(result, fmt.Sprintf("prefetchThreshold = %d", r.prefetchThreshold))
}
result = append(result, fmt.Sprintf("cache items count = %d", r.resultCache.TotalCount()))
return
}
// Resolve checks if the current query result is already in the cache and returns it
// or delegates to the next resolver
func (r *CachingResolver) Resolve(request *model.Request) (response *model.Response, err error) {
logger := withPrefix(request.Log, "caching_resolver")
if r.maxCacheTimeSec < 0 {
logger.Debug("skip cache")
return r.next.Resolve(request)
}
resp := new(dns.Msg)
resp.SetReply(request.Req)
for _, question := range request.Req.Question {
domain := util.ExtractDomain(question)
cacheKey := util.GenerateCacheKey(dns.Type(question.Qtype), domain)
logger := logger.WithField("domain", util.Obfuscate(domain))
r.trackQueryDomainNameCount(domain, cacheKey, logger)
val, ttl := r.resultCache.Get(cacheKey)
if val != nil {
logger.Debug("domain is cached")
evt.Bus().Publish(evt.CachingResultCacheHit, domain)
v, ok := val.(cacheValue)
if ok {
if v.prefetch {
// Hit from prefetch cache
evt.Bus().Publish(evt.CachingPrefetchCacheHit, domain)
}
// Answer from successful request
resp.Answer = v.answer
for _, rr := range resp.Answer {
rr.Header().Ttl = uint32(ttl.Seconds())
}
return &model.Response{Res: resp, RType: model.ResponseTypeCACHED, Reason: "CACHED"}, nil
}
// Answer with response code != OK
resp.Rcode = val.(int)
return &model.Response{Res: resp, RType: model.ResponseTypeCACHED, Reason: "CACHED NEGATIVE"}, nil
}
evt.Bus().Publish(evt.CachingResultCacheMiss, domain)
logger.WithField("next_resolver", Name(r.next)).Debug("not in cache: go to next resolver")
response, err = r.next.Resolve(request)
if err == nil {
r.putInCache(cacheKey, response, false, r.redisEnabled)
}
}
return response, err
}
func (r *CachingResolver) trackQueryDomainNameCount(domain string, cacheKey string, logger *logrus.Entry) {
if r.prefetchingNameCache != nil {
var domainCount int
if x, _ := r.prefetchingNameCache.Get(cacheKey); x != nil {
domainCount = x.(int)
}
domainCount++
r.prefetchingNameCache.Put(cacheKey, domainCount, r.prefetchExpires)
totalCount := r.prefetchingNameCache.TotalCount()
logger.Debugf("domain '%s' was requested %d times, "+
"total cache size: %d", util.Obfuscate(domain), domainCount, totalCount)
evt.Bus().Publish(evt.CachingDomainsToPrefetchCountChanged, totalCount)
}
}
func (r *CachingResolver) putInCache(cacheKey string, response *model.Response, prefetch, publish bool) {
answer := response.Res.Answer
if response.Res.Rcode == dns.RcodeSuccess {
// put value into cache
r.resultCache.Put(cacheKey, cacheValue{answer, prefetch}, r.adjustTTLs(answer))
} else if response.Res.Rcode == dns.RcodeNameError {
if r.cacheTimeNegative > 0 {
// put return code if NXDOMAIN
r.resultCache.Put(cacheKey, response.Res.Rcode, r.cacheTimeNegative)
}
}
evt.Bus().Publish(evt.CachingResultCacheChanged, r.resultCache.TotalCount())
if publish && r.redisClient != nil {
res := *response.Res
res.Answer = answer
r.redisClient.PublishCache(cacheKey, &res)
}
}
// adjustTTLs calculates and returns the max TTL (considers also the min and max cache time)
// for all records from answer or a negative cache time for empty answer
// adjust the TTL in the answer header accordingly
func (r *CachingResolver) adjustTTLs(answer []dns.RR) (maxTTL time.Duration) {
var max uint32
if len(answer) == 0 {
return r.cacheTimeNegative
}
for _, a := range answer {
// if TTL < mitTTL -> adjust the value, set minTTL
if r.minCacheTimeSec > 0 {
if a.Header().Ttl < uint32(r.minCacheTimeSec) {
a.Header().Ttl = uint32(r.minCacheTimeSec)
}
}
if r.maxCacheTimeSec > 0 {
if a.Header().Ttl > uint32(r.maxCacheTimeSec) {
a.Header().Ttl = uint32(r.maxCacheTimeSec)
}
}
if max < a.Header().Ttl {
max = a.Header().Ttl
}
}
return time.Duration(max) * time.Second
}