-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathdnscache.go
308 lines (265 loc) · 7.8 KB
/
dnscache.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package dnscache
import (
"context"
"net"
"net/http/httptrace"
"sync"
"time"
"golang.org/x/sync/singleflight"
)
type DNSResolver interface {
LookupHost(ctx context.Context, host string) (addrs []string, err error)
LookupAddr(ctx context.Context, addr string) (names []string, err error)
}
type Resolver struct {
// Timeout defines the maximum allowed time allowed for a lookup.
Timeout time.Duration
// Resolver is used to perform actual DNS lookup. If nil,
// net.DefaultResolver is used instead.
Resolver DNSResolver
once sync.Once
mu sync.RWMutex
cache map[string]*cacheEntry
// OnCacheMiss is executed if the host or address is not included in
// the cache and the default lookup is executed.
OnCacheMiss func()
}
type ResolverRefreshOptions struct {
ClearUnused bool
PersistOnFailure bool
}
type cacheEntry struct {
rrs []string
err error
used bool
}
// LookupAddr performs a reverse lookup for the given address, returning a list
// of names mapping to that address.
func (r *Resolver) LookupAddr(ctx context.Context, addr string) (names []string, err error) {
r.once.Do(r.init)
return r.lookup(ctx, "r"+addr)
}
// LookupHost looks up the given host using the local resolver. It returns a
// slice of that host's addresses.
func (r *Resolver) LookupHost(ctx context.Context, host string) (addrs []string, err error) {
r.once.Do(r.init)
return r.lookup(ctx, "h"+host)
}
// refreshRecords refreshes cached entries which have been used at least once since
// the last Refresh. If clearUnused is true, entries which haven't be used since the
// last Refresh are removed from the cache. If persistOnFailure is true, stale
// entries will not be removed on failed lookups
func (r *Resolver) refreshRecords(clearUnused bool, persistOnFailure bool) {
r.once.Do(r.init)
r.mu.RLock()
update := make([]string, 0, len(r.cache))
del := make([]string, 0, len(r.cache))
for key, entry := range r.cache {
if entry.used {
update = append(update, key)
} else if clearUnused {
del = append(del, key)
}
}
r.mu.RUnlock()
if len(del) > 0 {
r.mu.Lock()
for _, key := range del {
delete(r.cache, key)
}
r.mu.Unlock()
}
for _, key := range update {
r.update(context.Background(), key, false, persistOnFailure)
}
}
func (r *Resolver) Refresh(clearUnused bool) {
r.refreshRecords(clearUnused, false)
}
func (r *Resolver) RefreshWithOptions(options ResolverRefreshOptions) {
r.refreshRecords(options.ClearUnused, options.PersistOnFailure)
}
func (r *Resolver) init() {
r.cache = make(map[string]*cacheEntry)
}
// lookupGroup merges lookup calls together for lookups for the same host. The
// lookupGroup key is is the LookupIPAddr.host argument.
var lookupGroup singleflight.Group
func (r *Resolver) lookup(ctx context.Context, key string) (rrs []string, err error) {
var found bool
rrs, err, found = r.load(key)
if !found {
if r.OnCacheMiss != nil {
r.OnCacheMiss()
}
rrs, err = r.update(ctx, key, true, false)
}
return
}
func (r *Resolver) update(ctx context.Context, key string, used bool, persistOnFailure bool) (rrs []string, err error) {
c := lookupGroup.DoChan(key, r.lookupFunc(ctx, key))
select {
case <-ctx.Done():
err = ctx.Err()
if err == context.DeadlineExceeded {
// If DNS request timed out for some reason, force future
// request to start the DNS lookup again rather than waiting
// for the current lookup to complete.
lookupGroup.Forget(key)
}
case res := <-c:
if res.Shared {
// We had concurrent lookups, check if the cache is already updated
// by a friend.
var found bool
rrs, err, found = r.load(key)
if found {
return
}
}
err = res.Err
if err == nil {
rrs, _ = res.Val.([]string)
}
if err != nil && persistOnFailure {
var found bool
rrs, err, found = r.load(key)
if found {
return
}
}
r.mu.Lock()
r.storeLocked(key, rrs, used, err)
r.mu.Unlock()
}
return
}
// lookupFunc returns lookup function for key. The type of the key is stored as
// the first char and the lookup subject is the rest of the key.
func (r *Resolver) lookupFunc(ctx context.Context, key string) func() (interface{}, error) {
if len(key) == 0 {
panic("lookupFunc with empty key")
}
var resolver DNSResolver = defaultResolver
if r.Resolver != nil {
resolver = r.Resolver
}
switch key[0] {
case 'h':
return func() (interface{}, error) {
ctx, cancel := r.prepareCtx(ctx)
defer cancel()
return resolver.LookupHost(ctx, key[1:])
}
case 'r':
return func() (interface{}, error) {
ctx, cancel := r.prepareCtx(ctx)
defer cancel()
return resolver.LookupAddr(ctx, key[1:])
}
default:
panic("lookupFunc invalid key type: " + key)
}
}
func (r *Resolver) prepareCtx(origContext context.Context) (ctx context.Context, cancel context.CancelFunc) {
ctx = context.Background()
if r.Timeout > 0 {
ctx, cancel = context.WithTimeout(ctx, r.Timeout)
} else {
cancel = func() {}
}
// If a httptrace has been attached to the given context it will be copied over to the newly created context. We only need to copy pointers
// to DNSStart and DNSDone hooks
if trace := httptrace.ContextClientTrace(origContext); trace != nil {
derivedTrace := &httptrace.ClientTrace{
DNSStart: trace.DNSStart,
DNSDone: trace.DNSDone,
}
ctx = httptrace.WithClientTrace(ctx, derivedTrace)
}
return
}
func (r *Resolver) load(key string) (rrs []string, err error, found bool) {
r.mu.RLock()
var entry *cacheEntry
entry, found = r.cache[key]
if !found {
r.mu.RUnlock()
return
}
rrs = entry.rrs
err = entry.err
used := entry.used
r.mu.RUnlock()
if !used {
r.mu.Lock()
entry.used = true
r.mu.Unlock()
}
return rrs, err, true
}
func (r *Resolver) storeLocked(key string, rrs []string, used bool, err error) {
if entry, found := r.cache[key]; found {
// Update existing entry in place
entry.rrs = rrs
entry.err = err
entry.used = used
return
}
r.cache[key] = &cacheEntry{
rrs: rrs,
err: err,
used: used,
}
}
var defaultResolver = &defaultResolverWithTrace{
ipVersion: "ip",
}
// Create a new resolver that only resolves to IPv4 Addresses when looking up Hosts.
// Example:
//
// resolver := dnscache.Resolver{
// Resolver: NewResolverOnlyV4(),
// }
func NewResolverOnlyV4() DNSResolver {
return &defaultResolverWithTrace{
ipVersion: "ip4",
}
}
// Create a new resolver that only resolves to IPv6 Addresses when looking up Hosts.
// Example:
//
// resolver := dnscache.Resolver{
// Resolver: NewResolverOnlyV6(),
// }
func NewResolverOnlyV6() DNSResolver {
return &defaultResolverWithTrace{
ipVersion: "ip6",
}
}
// defaultResolverWithTrace calls `LookupIP` instead of `LookupHost` on `net.DefaultResolver` in order to cause invocation of the `DNSStart`
// and `DNSDone` hooks. By implementing `DNSResolver`, backward compatibility can be ensured.
type defaultResolverWithTrace struct {
ipVersion string
}
func (d *defaultResolverWithTrace) LookupHost(ctx context.Context, host string) (addrs []string, err error) {
ipVersion := d.ipVersion
if ipVersion != "ip" && ipVersion != "ip4" && ipVersion != "ip6" {
ipVersion = "ip"
}
// `net.Resolver#LookupHost` does not cause invocation of `net.Resolver#lookupIPAddr`, therefore the `DNSStart` and `DNSDone` tracing hooks
// built into the stdlib are never called. `LookupIP`, despite it's name, can also be used to lookup a hostname but does cause these hooks to be
// triggered. The format of the reponse is different, therefore it needs this thin wrapper converting it.
rawIPs, err := net.DefaultResolver.LookupIP(ctx, ipVersion, host)
if err != nil {
return nil, err
}
cookedIPs := make([]string, len(rawIPs))
for i, v := range rawIPs {
cookedIPs[i] = v.String()
}
return cookedIPs, nil
}
func (d *defaultResolverWithTrace) LookupAddr(ctx context.Context, addr string) (names []string, err error) {
return net.DefaultResolver.LookupAddr(ctx, addr)
}