-
Notifications
You must be signed in to change notification settings - Fork 245
/
parallel.go
174 lines (138 loc) · 4.16 KB
/
parallel.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
package upstream
import (
"context"
"net/netip"
"time"
"github.com/AdguardTeam/dnsproxy/internal/bootstrap"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
// ErrNoUpstreams is returned from the methods that expect at least a single
// upstream to work with when no upstreams specified.
const ErrNoUpstreams errors.Error = "no upstream specified"
// ExchangeParallel returns the dirst successful response from one of u. It
// returns an error if all upstreams failed to exchange the request.
func ExchangeParallel(u []Upstream, req *dns.Msg) (reply *dns.Msg, resolved Upstream, err error) {
upsNum := len(u)
switch upsNum {
case 0:
return nil, nil, ErrNoUpstreams
case 1:
reply, err = exchangeAndLog(u[0], req)
return reply, u[0], err
default:
// Go on.
}
ch := make(chan *exchangeResult, upsNum)
for _, f := range u {
go exchangeAsync(f, req, ch)
}
errs := []error{}
for range u {
rep := <-ch
if rep.err != nil {
errs = append(errs, rep.err)
continue
}
if rep.reply != nil {
return rep.reply, rep.upstream, nil
}
}
if len(errs) == 0 {
return nil, nil, errors.Error("none of upstream servers responded")
}
// TODO(e.burkov): Use [errors.Join] in Go 1.20.
return nil, nil, errors.List("all upstreams failed to respond", errs...)
}
// ExchangeAllResult is the successful result of [ExchangeAll] for a single
// upstream.
type ExchangeAllResult struct {
// Resp is the response DNS request resolved into.
Resp *dns.Msg
// Upstream is the upstream that successfully resolved the request.
Upstream Upstream
}
// ExchangeAll retunrs the responses from all of u. It returns an error only if
// all upstreams failed to exchange the request.
func ExchangeAll(ups []Upstream, req *dns.Msg) (res []ExchangeAllResult, err error) {
upsl := len(ups)
switch upsl {
case 0:
return nil, ErrNoUpstreams
case 1:
var reply *dns.Msg
reply, err = exchangeAndLog(ups[0], req)
if err != nil {
return nil, err
} else if reply == nil {
return nil, errors.Error("no reply")
}
return []ExchangeAllResult{{Upstream: ups[0], Resp: reply}}, nil
default:
// Go on.
}
res = make([]ExchangeAllResult, 0, upsl)
errs := make([]error, 0, upsl)
resCh := make(chan *exchangeResult, upsl)
// Start exchanging concurrently.
for _, u := range ups {
go exchangeAsync(u, req, resCh)
}
// Wait for all exchanges to finish.
for range ups {
rep := <-resCh
if rep.err != nil {
errs = append(errs, rep.err)
continue
}
if rep.reply == nil {
errs = append(errs, errors.Error("no reply"))
continue
}
res = append(res, ExchangeAllResult{
Resp: rep.reply,
Upstream: rep.upstream,
})
}
if len(errs) == upsl {
// TODO(e.burkov): Use [errors.Join] in Go 1.20.
return res, errors.List("all upstreams failed to exchange", errs...)
}
return res, nil
}
// exchangeResult represents the result of DNS exchange.
type exchangeResult = struct {
// upstream is the Upstream that successfully resolved the request.
upstream Upstream
// reply is the response DNS request resolved into.
reply *dns.Msg
// err is the error that occurred while resolving the request.
err error
}
// exchangeAsync tries to resolve DNS request with one upstream and sends the
// result to respCh.
func exchangeAsync(u Upstream, req *dns.Msg, respCh chan *exchangeResult) {
res := &exchangeResult{upstream: u}
res.reply, res.err = exchangeAndLog(u, req)
respCh <- res
}
// exchangeAndLog wraps the [Upstream.Exchange] method with logging.
func exchangeAndLog(u Upstream, req *dns.Msg) (resp *dns.Msg, err error) {
addr := u.Address()
req = req.Copy()
start := time.Now()
reply, err := u.Exchange(req)
elapsed := time.Since(start)
if q := &req.Question[0]; err == nil {
log.Debug("upstream %s exchanged %s successfully in %s", addr, q, elapsed)
} else {
log.Debug("upstream %s failed to exchange %s in %s: %s", addr, q, elapsed, err)
}
return reply, err
}
// LookupParallel tries to lookup for ip of host with all resolvers
// concurrently.
func LookupParallel(ctx context.Context, resolvers []Resolver, host string) ([]netip.Addr, error) {
return bootstrap.LookupParallel(ctx, resolvers, host)
}