-
Notifications
You must be signed in to change notification settings - Fork 11
/
cache.go
155 lines (129 loc) · 2.98 KB
/
cache.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
package dns
import (
"context"
"math/rand"
"sync"
"time"
)
// Cache is a DNS query cache handler.
type Cache struct {
mu sync.RWMutex
cache map[Question]*Message
}
// ServeDNS answers query questions from a local cache, and forwards unanswered
// questions upstream, then caches the answers from the response.
func (c *Cache) ServeDNS(ctx context.Context, w MessageWriter, r *Query) {
var (
miss bool
now = time.Now()
)
c.mu.RLock()
for _, q := range r.Questions {
if hit := c.lookup(q, w, now); !hit {
miss = true
}
}
c.mu.RUnlock()
if !miss {
return
}
msg, err := w.Recur(ctx)
if err != nil || msg == nil {
w.Status(ServFail)
return
}
if msg.RCode == NoError {
c.insert(msg, now)
}
writeMessage(w, msg)
}
// c.mu.RLock held
func (c *Cache) lookup(q Question, w MessageWriter, now time.Time) bool {
msg, ok := c.cache[q]
if !ok {
return false
}
var answers, authorities, additionals []Resource
for _, res := range msg.Answers {
if res.TTL = cacheTTL(res.TTL, now); res.TTL <= 0 {
return false
}
answers = append(answers, res)
}
for _, res := range msg.Authorities {
if res.TTL = cacheTTL(res.TTL, now); res.TTL <= 0 {
return false
}
authorities = append(authorities, res)
}
for _, res := range msg.Additionals {
if res.TTL = cacheTTL(res.TTL, now); res.TTL <= 0 {
return false
}
additionals = append(additionals, res)
}
randomize(answers)
for _, res := range answers {
w.Answer(res.Name, res.TTL, res.Record)
}
for _, res := range authorities {
w.Authority(res.Name, res.TTL, res.Record)
}
for _, res := range additionals {
w.Additional(res.Name, res.TTL, res.Record)
}
return true
}
func (c *Cache) insert(msg *Message, now time.Time) {
cache := make(map[Question]*Message, len(msg.Questions))
for _, q := range msg.Questions {
m := new(Message)
for _, res := range msg.Answers {
res.TTL = cacheEpoch(res.TTL, now)
m.Answers = append(m.Answers, res)
}
for _, res := range msg.Authorities {
res.TTL = cacheEpoch(res.TTL, now)
m.Authorities = append(m.Authorities, res)
}
for _, res := range msg.Additionals {
res.TTL = cacheEpoch(res.TTL, now)
m.Additionals = append(m.Additionals, res)
}
cache[q] = m
}
c.mu.Lock()
defer c.mu.Unlock()
if c.cache == nil {
c.cache = cache
return
}
for q, m := range cache {
c.cache[q] = m
}
}
func cacheEpoch(ttl time.Duration, now time.Time) time.Duration {
return time.Duration(now.Add(ttl).UnixNano())
}
func cacheTTL(epoch time.Duration, now time.Time) time.Duration {
return time.Unix(0, int64(epoch)).Sub(now)
}
// randomize shuffles contigous groups of resourcesfor the same name.
func randomize(s []Resource) {
var low, high int
for low = 0; low < len(s)-1; low++ {
for high = low + 1; high < len(s) && s[low].Name == s[high].Name; high++ {
}
shuffle(s[low:high])
low = high
}
}
func shuffle(s []Resource) {
if len(s) < 2 {
return
}
for i := len(s) - 1; i > 0; i-- {
j := rand.Intn(i + 1)
s[i], s[j] = s[j], s[i]
}
}