forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdnscache.go
319 lines (276 loc) · 8.47 KB
/
rdnscache.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
309
310
311
312
313
314
315
316
317
318
319
package reverse_dns
import (
"context"
"errors"
"net"
"sync"
"sync/atomic"
"time"
"golang.org/x/sync/semaphore"
)
const defaultMaxWorkers = 10
var (
ErrTimeout = errors.New("request timed out")
)
// AnyResolver is for the net.Resolver
type AnyResolver interface {
LookupAddr(ctx context.Context, addr string) (names []string, err error)
}
// ReverseDNSCache is safe to use across multiple goroutines.
// if multiple goroutines request the same IP at the same time, one of the
// requests will trigger the lookup and the rest will wait for its response.
type ReverseDNSCache struct {
Resolver AnyResolver
stats RDNSCacheStats
// settings
ttl time.Duration
lookupTimeout time.Duration
maxWorkers int
// internal
rwLock sync.RWMutex
sem *semaphore.Weighted
cancelCleanupWorker context.CancelFunc
cache map[string]*dnslookup
// keep an ordered list of what needs to be worked on and what is due to expire.
// We can use this list for both with a job position marker, and by popping items
// off the list as they expire. This avoids iterating over the whole map to find
// things to do.
// As a bonus, we only have to read the first item to know if anything in the
// map has expired.
// must lock to get access to this.
expireList []*dnslookup
expireListLock sync.Mutex
}
type RDNSCacheStats struct {
CacheHit uint64
CacheMiss uint64
CacheExpire uint64
RequestsAbandoned uint64
RequestsFilled uint64
}
func NewReverseDNSCache(ttl, lookupTimeout time.Duration, workerPoolSize int) *ReverseDNSCache {
if workerPoolSize <= 0 {
workerPoolSize = defaultMaxWorkers
}
ctx, cancel := context.WithCancel(context.Background())
d := &ReverseDNSCache{
ttl: ttl,
lookupTimeout: lookupTimeout,
cache: map[string]*dnslookup{},
expireList: []*dnslookup{},
maxWorkers: workerPoolSize,
sem: semaphore.NewWeighted(int64(workerPoolSize)),
cancelCleanupWorker: cancel,
Resolver: net.DefaultResolver,
}
d.startCleanupWorker(ctx)
return d
}
// dnslookup represents a lookup request/response. It may or may not be answered yet.
// interested parties register themselves with existing requests or create new ones
// to get their dns query answered. Answers will be pushed out to callbacks.
type dnslookup struct {
ip string // keep a copy for the expireList.
domains []string
expiresAt time.Time
completed bool
callbacks []callbackChannelType
}
type lookupResult struct {
domains []string
err error
}
type callbackChannelType chan lookupResult
// Lookup takes a string representing a parseable ipv4 or ipv6 IP, and blocks
// until it has resolved to 0-n results, or until its lookup timeout has elapsed.
// if the lookup timeout elapses, it returns an empty slice.
func (d *ReverseDNSCache) Lookup(ip string) ([]string, error) {
if len(ip) == 0 {
return nil, nil
}
return d.lookup(ip)
}
func (d *ReverseDNSCache) lookup(ip string) ([]string, error) {
// check if the value is cached
d.rwLock.RLock()
result, found := d.lockedGetFromCache(ip)
if found && result.completed && result.expiresAt.After(time.Now()) {
defer d.rwLock.RUnlock()
atomic.AddUint64(&d.stats.CacheHit, 1)
// cache is valid
return result.domains, nil
}
d.rwLock.RUnlock()
// if it's not cached, kick off a lookup job and subscribe to the result.
lookupChan := d.subscribeTo(ip)
timer := time.NewTimer(d.lookupTimeout)
defer timer.Stop()
// timer is still necessary even if doLookup respects timeout due to worker
// pool starvation.
select {
case result := <-lookupChan:
return result.domains, result.err
case <-timer.C:
return nil, ErrTimeout
}
}
func (d *ReverseDNSCache) subscribeTo(ip string) callbackChannelType {
callback := make(callbackChannelType, 1)
d.rwLock.Lock()
defer d.rwLock.Unlock()
// confirm it's still not in the cache. This needs to be done under an active lock.
result, found := d.lockedGetFromCache(ip)
if found {
atomic.AddUint64(&d.stats.CacheHit, 1)
// has the request been answered since we last checked?
if result.completed {
// we can return the answer with the channel.
callback <- lookupResult{domains: result.domains}
return callback
}
// there's a request but it hasn't been answered yet;
// add yourself to the subscribers and return that.
result.callbacks = append(result.callbacks, callback)
d.lockedSaveToCache(result)
return callback
}
atomic.AddUint64(&d.stats.CacheMiss, 1)
// otherwise we need to register the request
l := &dnslookup{
ip: ip,
expiresAt: time.Now().Add(d.ttl),
callbacks: []callbackChannelType{callback},
}
d.lockedSaveToCache(l)
go d.doLookup(l.ip)
return callback
}
// lockedGetFromCache fetches from the correct internal ip cache.
// you MUST first do a read or write lock before calling it, and keep locks around
// the dnslookup that is returned until you clone it.
func (d *ReverseDNSCache) lockedGetFromCache(ip string) (lookup *dnslookup, found bool) {
lookup, found = d.cache[ip]
if found && lookup.expiresAt.Before(time.Now()) {
return nil, false
}
return lookup, found
}
// lockedSaveToCache stores a lookup in the correct internal ip cache.
// you MUST first do a write lock before calling it.
func (d *ReverseDNSCache) lockedSaveToCache(lookup *dnslookup) {
if lookup.expiresAt.Before(time.Now()) {
return // don't cache.
}
d.cache[lookup.ip] = lookup
}
func (d *ReverseDNSCache) startCleanupWorker(ctx context.Context) {
go func() {
cleanupTick := time.NewTicker(10 * time.Second)
for {
select {
case <-cleanupTick.C:
d.cleanup()
case <-ctx.Done():
return
}
}
}()
}
func (d *ReverseDNSCache) doLookup(ip string) {
ctx, cancel := context.WithTimeout(context.Background(), d.lookupTimeout)
defer cancel()
if err := d.sem.Acquire(ctx, 1); err != nil {
// lookup timeout
d.abandonLookup(ip, ErrTimeout)
return
}
defer d.sem.Release(1)
names, err := d.Resolver.LookupAddr(ctx, ip)
if err != nil {
d.abandonLookup(ip, err)
return
}
d.rwLock.Lock()
lookup, found := d.lockedGetFromCache(ip)
if !found {
d.rwLock.Unlock()
return
}
lookup.domains = names
lookup.completed = true
lookup.expiresAt = time.Now().Add(d.ttl) // extend the ttl now that we have a reply.
callbacks := lookup.callbacks
lookup.callbacks = nil
d.lockedSaveToCache(lookup)
d.rwLock.Unlock()
d.expireListLock.Lock()
// add it to the expireList.
d.expireList = append(d.expireList, lookup)
d.expireListLock.Unlock()
atomic.AddUint64(&d.stats.RequestsFilled, uint64(len(callbacks)))
for _, cb := range callbacks {
cb <- lookupResult{domains: names}
close(cb)
}
}
func (d *ReverseDNSCache) abandonLookup(ip string, err error) {
d.rwLock.Lock()
lookup, found := d.lockedGetFromCache(ip)
if !found {
d.rwLock.Unlock()
return
}
callbacks := lookup.callbacks
delete(d.cache, lookup.ip)
d.rwLock.Unlock()
// resolve the remaining callbacks to free the resources.
atomic.AddUint64(&d.stats.RequestsAbandoned, uint64(len(callbacks)))
for _, cb := range callbacks {
cb <- lookupResult{err: err}
close(cb)
}
}
func (d *ReverseDNSCache) cleanup() {
now := time.Now()
d.expireListLock.Lock()
if len(d.expireList) == 0 {
d.expireListLock.Unlock()
return
}
ipsToDelete := []string{}
for i := 0; i < len(d.expireList); i++ {
if d.expireList[i].expiresAt.After(now) {
break // done. Nothing after this point is expired.
}
ipsToDelete = append(ipsToDelete, d.expireList[i].ip)
}
if len(ipsToDelete) == 0 {
d.expireListLock.Unlock()
return
}
d.expireList = d.expireList[len(ipsToDelete):]
d.expireListLock.Unlock()
atomic.AddUint64(&d.stats.CacheExpire, uint64(len(ipsToDelete)))
d.rwLock.Lock()
defer d.rwLock.Unlock()
for _, ip := range ipsToDelete {
delete(d.cache, ip)
}
}
// blockAllWorkers is a test function that eats up all the worker pool space to
// make sure workers are done running and there's no room to acquire a new worker.
func (d *ReverseDNSCache) blockAllWorkers() {
d.sem.Acquire(context.Background(), int64(d.maxWorkers))
}
func (d *ReverseDNSCache) Stats() RDNSCacheStats {
stats := RDNSCacheStats{}
stats.CacheHit = atomic.LoadUint64(&d.stats.CacheHit)
stats.CacheMiss = atomic.LoadUint64(&d.stats.CacheMiss)
stats.CacheExpire = atomic.LoadUint64(&d.stats.CacheExpire)
stats.RequestsAbandoned = atomic.LoadUint64(&d.stats.RequestsAbandoned)
stats.RequestsFilled = atomic.LoadUint64(&d.stats.RequestsFilled)
return stats
}
func (d *ReverseDNSCache) Stop() {
d.cancelCleanupWorker()
}