-
Notifications
You must be signed in to change notification settings - Fork 5
/
dnscache.go
119 lines (110 loc) · 3.18 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
package walker
import (
"net"
"sync"
"time"
"github.com/dropbox/godropbox/container/lrucache"
)
//TODO:
// - use a time-based cache instead of entry-capped, since we know we'll
// need most of the recently-accessed domains and few of the aging entries
// - consider not caching failures or doing any blacklisting here; the more
// likely usecase will be to retry a few times (in which case we don't want
// caching) and then not bother crawling this host at all
// DNSCachingDial wraps the given dial function with Caching of DNS
// resolutions. When a hostname is found in the cache it will call the provided
// dial with the IP address instead of the hostname, so no DNS lookup need be
// performed. It will also cache DNS failures.
//
func DNSCachingDial(dial func(network, addr string) (net.Conn, error), maxEntries int) func(network, addr string) (net.Conn, error) {
if dial == nil {
dial = net.Dial
}
c := &dnsCache{
wrappedDial: dial,
cache: lrucache.New(maxEntries),
}
return c.dial
}
// dnsCache wraps a net.Dial-type function with it's own version that will
// cache DNS entries in an LRU cache.
type dnsCache struct {
wrappedDial func(network, address string) (net.Conn, error)
cache *lrucache.LRUCache
mu sync.RWMutex
}
type hostrecord struct {
ipaddr string
blacklisted bool
err error
lastQuery time.Time
}
func (c *dnsCache) dial(network, addr string) (net.Conn, error) {
mapEntryName := network + addr
c.mu.RLock()
if entry, ok := c.cache.Get(mapEntryName); ok {
record := entry.(hostrecord)
lastQueryTime := record.lastQuery
if time.Since(lastQueryTime) > 5*time.Minute {
c.mu.RUnlock()
c.cacheHost(network, addr)
c.mu.RLock()
entry, _ = c.cache.Get(mapEntryName)
record = entry.(hostrecord)
}
resolvedAddr := record.ipaddr
if record.blacklisted {
returnErr := record.err
c.mu.RUnlock()
return nil, returnErr
} else {
c.mu.RUnlock()
return c.wrappedDial(network, resolvedAddr)
}
} else {
c.mu.RUnlock()
return c.cacheHost(network, addr)
}
}
// cacheHost caches the DNS lookup for this host, overwriting any entry
// that may have previously existed.
func (c *dnsCache) cacheHost(network, addr string) (net.Conn, error) {
mapEntryName := network + addr
newConn, err := c.wrappedDial(network, addr)
queryTime := time.Now()
c.mu.Lock()
if err != nil {
c.cache.Set(mapEntryName, hostrecord{
ipaddr: "",
blacklisted: true,
err: err,
lastQuery: queryTime,
})
c.mu.Unlock()
return nil, err
} else {
remoteipaddr := newConn.RemoteAddr().String()
c.cache.Set(mapEntryName, hostrecord{
ipaddr: remoteipaddr,
blacklisted: false,
err: nil,
lastQuery: queryTime,
})
c.mu.Unlock()
return newConn, nil
}
}
// get returns the hostrecord associated with the passed network:address, if it exists.
// The second return value represents whether the record exists.
func (c *dnsCache) get(network, addr string) (hostrecord, bool) {
key := network + addr
c.mu.RLock()
valinterface, ok := c.cache.Get(key)
if valinterface == nil {
c.mu.RUnlock()
return hostrecord{}, ok
}
val := valinterface.(hostrecord)
c.mu.RUnlock()
return val, ok
}