-
Notifications
You must be signed in to change notification settings - Fork 178
/
dns_cache.go
298 lines (247 loc) · 8.05 KB
/
dns_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
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
package herocache
import (
"fmt"
"net"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module"
"github.com/onflow/flow-go/module/mempool"
herocache "github.com/onflow/flow-go/module/mempool/herocache/backdata"
"github.com/onflow/flow-go/module/mempool/herocache/backdata/heropool"
"github.com/onflow/flow-go/module/mempool/stdmap"
)
type DNSCache struct {
ipCache *stdmap.Backend
txtCache *stdmap.Backend
}
func NewDNSCache(sizeLimit uint32, logger zerolog.Logger, ipCollector module.HeroCacheMetrics, txtCollector module.HeroCacheMetrics) *DNSCache {
return &DNSCache{
txtCache: stdmap.NewBackend(
stdmap.WithBackData(
herocache.NewCache(
sizeLimit,
herocache.DefaultOversizeFactor,
heropool.LRUEjection,
logger.With().Str("mempool", "dns-txt-cache").Logger(),
txtCollector))),
ipCache: stdmap.NewBackend(
stdmap.WithBackData(
herocache.NewCache(
sizeLimit,
herocache.DefaultOversizeFactor,
heropool.LRUEjection,
logger.With().Str("mempool", "dns-ip-cache").Logger(),
ipCollector))),
}
}
// PutIpDomain adds the given ip domain into the cache.
func (d *DNSCache) PutIpDomain(domain string, addresses []net.IPAddr, timestamp int64) bool {
i := ipEntity{
IpRecord: mempool.IpRecord{
Domain: domain,
Addresses: addresses,
Timestamp: timestamp,
Locked: false,
},
id: domainToIdentifier(domain),
}
return d.ipCache.Add(i)
}
// PutTxtRecord adds the given txt record into the cache.
func (d *DNSCache) PutTxtRecord(domain string, record []string, timestamp int64) bool {
t := txtEntity{
TxtRecord: mempool.TxtRecord{
Txt: domain,
Records: record,
Timestamp: timestamp,
Locked: false,
},
id: domainToIdentifier(domain),
}
return d.txtCache.Add(t)
}
// GetDomainIp returns the ip domain if exists in the cache.
// The boolean return value determines if domain exists in the cache.
func (d *DNSCache) GetDomainIp(domain string) (*mempool.IpRecord, bool) {
entity, ok := d.ipCache.ByID(domainToIdentifier(domain))
if !ok {
return nil, false
}
i, ok := entity.(ipEntity)
if !ok {
return nil, false
}
ipRecord := i.IpRecord
return &ipRecord, true
}
// GetTxtRecord returns the txt record if exists in the cache.
// The boolean return value determines if record exists in the cache.
func (d *DNSCache) GetTxtRecord(domain string) (*mempool.TxtRecord, bool) {
entity, ok := d.txtCache.ByID(domainToIdentifier(domain))
if !ok {
return nil, false
}
t, ok := entity.(txtEntity)
if !ok {
return nil, false
}
txtRecord := t.TxtRecord
return &txtRecord, true
}
// RemoveIp removes an ip domain from cache.
func (d *DNSCache) RemoveIp(domain string) bool {
return d.ipCache.Remove(domainToIdentifier(domain))
}
// RemoveTxt removes a txt record from cache.
func (d *DNSCache) RemoveTxt(domain string) bool {
return d.txtCache.Remove(domainToIdentifier(domain))
}
// LockIPDomain locks an ip address dns record if exists in the cache.
// The boolean return value determines whether attempt on locking was successful.
//
// A locking attempt is successful when the domain record exists in the cache and has not
// been locked before.
// Once a domain record gets locked the only way to unlock it is through updating that record.
//
// The locking process is defined to record that a resolving attempt is ongoing for an expired domain.
// So the locking happens to avoid any other parallel resolving
func (d *DNSCache) LockIPDomain(domain string) (bool, error) {
locked := false
err := d.ipCache.Run(func(backdata mempool.BackData) error {
id := domainToIdentifier(domain)
entity, ok := backdata.ByID(id)
if !ok {
return fmt.Errorf("ip record does not exist in cache for locking: %s", domain)
}
record, ok := entity.(ipEntity)
if !ok {
return fmt.Errorf("unexpected type retrieved, expected: %T, obtained: %T", ipEntity{}, entity)
}
if record.Locked {
return nil // record has already been locked
}
record.Locked = true
if _, removed := backdata.Remove(id); !removed {
return fmt.Errorf("ip record could not be removed from backdata")
}
if added := backdata.Add(id, record); !added {
return fmt.Errorf("updated ip record could not be added to back data")
}
locked = record.Locked
return nil
})
return locked, err
}
// UpdateIPDomain updates the dns record for the given ip domain with the new address and timestamp values.
func (d *DNSCache) UpdateIPDomain(domain string, addresses []net.IPAddr, timestamp int64) error {
return d.ipCache.Run(func(backdata mempool.BackData) error {
id := domainToIdentifier(domain)
// removes old entry if exists.
backdata.Remove(id)
ipRecord := ipEntity{
IpRecord: mempool.IpRecord{
Domain: domain,
Addresses: addresses,
Timestamp: timestamp,
Locked: false, // by default an ip record is unlocked.
},
id: id,
}
if added := backdata.Add(id, ipRecord); !added {
return fmt.Errorf("updated ip record could not be added to backdata")
}
return nil
})
}
// UpdateTxtRecord updates the dns record for the given txt domain with the new address and timestamp values.
func (d *DNSCache) UpdateTxtRecord(txt string, records []string, timestamp int64) error {
return d.txtCache.Run(func(backdata mempool.BackData) error {
id := domainToIdentifier(txt)
// removes old entry if exists.
backdata.Remove(id)
txtRecord := txtEntity{
TxtRecord: mempool.TxtRecord{
Txt: txt,
Records: records,
Timestamp: timestamp,
Locked: false, // by default a txt record is unlocked.
},
id: id,
}
if added := backdata.Add(id, txtRecord); !added {
return fmt.Errorf("updated txt record could not be added to backdata")
}
return nil
})
}
// LockTxtRecord locks a txt address dns record if exists in the cache.
// The boolean return value determines whether attempt on locking was successful.
//
// A locking attempt is successful when the domain record exists in the cache and has not
// been locked before.
// Once a domain record gets locked the only way to unlock it is through updating that record.
//
// The locking process is defined to record that a resolving attempt is ongoing for an expired domain.
// So the locking happens to avoid any other parallel resolving.
func (d *DNSCache) LockTxtRecord(txt string) (bool, error) {
locked := false
err := d.txtCache.Run(func(backdata mempool.BackData) error {
id := domainToIdentifier(txt)
entity, ok := backdata.ByID(id)
if !ok {
return fmt.Errorf("txt record does not exist in cache for locking: %s", txt)
}
record, ok := entity.(txtEntity)
if !ok {
return fmt.Errorf("unexpected type retrieved, expected: %T, obtained: %T", txtEntity{}, entity)
}
if record.Locked {
return nil // record has already been locked
}
record.Locked = true
if _, removed := backdata.Remove(id); !removed {
return fmt.Errorf("txt record could not be removed from backdata")
}
if added := backdata.Add(id, record); !added {
return fmt.Errorf("updated txt record could not be added to back data")
}
locked = record.Locked
return nil
})
return locked, err
}
// Size returns total domains maintained into this cache.
// The first returned value determines number of ip domains.
// The second returned value determines number of txt records.
func (d DNSCache) Size() (uint, uint) {
return d.ipCache.Size(), d.txtCache.Size()
}
// ipEntity is a dns cache entry for ip records.
type ipEntity struct {
mempool.IpRecord
// caching identifier to avoid cpu overhead
// per query.
id flow.Identifier
}
func (i ipEntity) ID() flow.Identifier {
return i.id
}
func (i ipEntity) Checksum() flow.Identifier {
return domainToIdentifier(i.IpRecord.Domain)
}
// txtEntity is a dns cache entry for txt records.
type txtEntity struct {
mempool.TxtRecord
// caching identifier to avoid cpu overhead
// per query.
id flow.Identifier
}
func (t txtEntity) ID() flow.Identifier {
return t.id
}
func (t txtEntity) Checksum() flow.Identifier {
return domainToIdentifier(t.TxtRecord.Txt)
}
func domainToIdentifier(domain string) flow.Identifier {
return flow.MakeID(domain)
}