Repository navigation
Expand file tree
/
Copy pathcache.go
More file actions
244 lines (197 loc) · 5.63 KB
/
Copy pathcache.go
File metadata and controls
244 lines (197 loc) · 5.63 KB
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
package emailfinder
import (
"bytes"
"context"
"errors"
"github.com/dgraph-io/badger/v4"
"github.com/rs/zerolog"
)
const (
catchAllKey = "CATCH_ALL"
noMXRecordsKey = "NO_MX_RECORDS"
)
var _ Cache = &BadgerCache{}
type BadgerCache struct {
cache *badger.DB
}
func NewCache(cache *badger.DB) *BadgerCache {
return &BadgerCache{cache: cache}
}
type CachedDomain struct {
Domain string `json:"domain"`
Users []User `json:"users"`
CatchAll bool `json:"catchAll"`
NoMXRecords bool `json:"noMXRecords"`
}
func (e *BadgerCache) Store(res searchDomainRes) error {
if res.isCatchAll {
return e.cache.Update(func(txn *badger.Txn) error {
return txn.Set(cacheKey(res.domain, catchAllKey), nil)
})
}
errs := errors.Join(res.errors...)
if errors.Is(ErrNoMXRecords, errs) {
return e.cache.Update(func(txn *badger.Txn) error {
return txn.Set(cacheKey(res.domain, noMXRecordsKey), nil)
})
}
if len(res.reachableUsers) == 0 {
return nil
}
txn := e.cache.NewTransaction(true)
for _, v := range res.reachableUsers {
key := cacheKey(res.domain, v.Name)
val := []byte(v.Email)
if err := txn.Set(key, val); err == badger.ErrTxnTooBig {
if err := txn.Commit(); err != nil {
return err
}
txn = e.cache.NewTransaction(true)
if err := txn.Set(key, val); err != nil {
return err
}
}
}
return txn.Commit()
}
// ReadUsers returns names that were not foun in cache and sends cache hits to inCh
func (e *BadgerCache) ReadUsers(c context.Context, search DomainSearch, inCh chan<- reachableUser) []string {
var namesToSearch []string
err := e.cache.View(func(txn *badger.Txn) error {
for _, name := range search.Names {
key := cacheKey(search.Domain, name)
item, err := txn.Get(key)
if errors.Is(err, badger.ErrKeyNotFound) {
namesToSearch = append(namesToSearch, name)
continue
}
if err != nil {
zerolog.Ctx(c).Err(err).Send()
namesToSearch = append(namesToSearch, name)
continue
}
err = item.Value(func(val []byte) error {
zerolog.Ctx(c).Debug().Msgf("Found %s by %s in cache", val, key)
select {
case <-c.Done():
return nil
case inCh <- reachableUser{name: name, email: string(val)}:
}
return nil
})
if err != nil {
namesToSearch = append(namesToSearch, name)
zerolog.Ctx(c).Err(err).Send()
}
}
return nil
})
if err != nil {
zerolog.Ctx(c).Err(err).Send()
}
zerolog.Ctx(c).Debug().Strs("names_to_search", namesToSearch).
Msgf("Found %d names in cache, searching for %d remaining", len(search.Names)-len(namesToSearch), len(namesToSearch))
return namesToSearch
}
func (e *BadgerCache) CachedDomains(c context.Context) ([]CachedDomain, error) {
domains, err := e.cachedDomains()
if err != nil {
return nil, err
}
result := make([]CachedDomain, 0, len(domains))
for domain, cache := range domains {
if cache.NoMXRecords && (len(cache.Users) > 0 || cache.CatchAll) {
zerolog.Ctx(c).Error().Msgf("Inconsistent cache state for %s: noMXRecords=true but users or catchAll present", domain)
}
if cache.CatchAll && (len(cache.Users) > 0 || cache.NoMXRecords) {
zerolog.Ctx(c).Error().Msgf("Inconsistent cache state for %s: catchAll=true but users or noMXRecords present", domain)
}
result = append(result, CachedDomain{
Domain: domain,
Users: cache.Users,
CatchAll: cache.CatchAll,
NoMXRecords: cache.NoMXRecords,
})
}
return result, nil
}
func (e *BadgerCache) cachedDomains() (map[string]*CachedDomain, error) {
cachedDomains := make(map[string]*CachedDomain)
return cachedDomains, e.cache.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 50
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
k := item.Key()
err := item.Value(func(v []byte) error {
domain, name := cacheKeyUnpack(k)
if _, ok := cachedDomains[domain]; !ok {
cachedDomains[domain] = new(CachedDomain)
}
switch name {
case catchAllKey:
cachedDomains[domain].CatchAll = true
case noMXRecordsKey:
cachedDomains[domain].NoMXRecords = true
default:
email := string(v)
cachedDomains[domain].Users = append(cachedDomains[domain].Users, User{Name: name, Email: email})
}
return nil
})
if err != nil {
return err
}
}
return nil
})
}
func (e *BadgerCache) CatchAll(domain string) (bool, error) {
return e.cachedKeyExist(cacheKey(domain, catchAllKey))
}
func (e *BadgerCache) NoMXRecords(domain string) (bool, error) {
return e.cachedKeyExist(cacheKey(domain, noMXRecordsKey))
}
func (e *BadgerCache) cachedKeyExist(key []byte) (bool, error) {
err := e.cache.View(func(txn *badger.Txn) error {
_, err := txn.Get(key)
return err
})
if err == badger.ErrKeyNotFound {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}
func cacheKey(domain, name string) []byte {
return []byte(domain + "#" + name)
}
func cacheKeyUnpack(key []byte) (domain string, name string) {
parts := bytes.Split(key, []byte{'#'})
if len(parts) != 2 {
panic("invalid key format")
}
domain = string(parts[0])
name = string(parts[1])
return
}
var _ Cache = &NoopCache{}
type NoopCache struct{}
func (e NoopCache) Store(res searchDomainRes) error {
return nil
}
func (e NoopCache) ReadUsers(c context.Context, search DomainSearch, inCh chan<- reachableUser) []string {
return search.Names
}
func (e NoopCache) CachedDomains() ([]CachedDomain, error) {
return nil, nil
}
func (e NoopCache) CatchAll(domain string) (bool, error) {
return false, nil
}
func (e NoopCache) NoMXRecords(domain string) (bool, error) {
return false, nil
}