-
Notifications
You must be signed in to change notification settings - Fork 4
/
resolverdns.go
74 lines (58 loc) · 1.34 KB
/
resolverdns.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
package resolvermt
import (
"github.com/miekg/dns"
)
type resolverDNS struct {
serverBalancer balancer
parser messageParser
retryCodes map[int]struct{}
retryCount int
}
type balancer interface {
Next() server
Close()
}
type messageParser interface {
Parse(msg *dns.Msg) []Record
}
func newResolverDNS(retryCount int, retryCodes []int, serverBalancer balancer, parser messageParser) *resolverDNS {
retryMap := make(map[int]struct{}, len(retryCodes))
for _, code := range retryCodes {
retryMap[code] = struct{}{}
}
return &resolverDNS{
serverBalancer: serverBalancer,
parser: parser,
retryCodes: retryMap,
retryCount: retryCount,
}
}
func (s *resolverDNS) Resolve(query string, rrtype RRtype) []Record {
var err error
var msg *dns.Msg
// Send the request to a server, retrying on error
var i int
for i = 0; i < s.retryCount; i++ {
server := s.serverBalancer.Next()
msg, _, err = server.Query(query, rrtype)
if err != nil {
continue
}
if _, ok := s.retryCodes[msg.Rcode]; ok {
continue
}
break
}
// Max retry count reached
if i == s.retryCount {
return nil
}
// Parse records only on success or NXDOMAIN
if msg.Rcode != dns.RcodeSuccess && msg.Rcode != dns.RcodeNameError {
return nil
}
return s.parser.Parse(msg)
}
func (s *resolverDNS) Close() {
s.serverBalancer.Close()
}