/
parallel.go
184 lines (156 loc) · 4.49 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
175
176
177
178
179
180
181
182
183
184
package upstream
import (
"context"
"fmt"
"net/netip"
"slices"
"time"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/log"
"github.com/miekg/dns"
)
const (
// ErrNoUpstreams is returned from the methods that expect at least a single
// upstream to work with when no upstreams specified.
ErrNoUpstreams errors.Error = "no upstream specified"
// ErrNoReply is returned from [ExchangeAll] when no upstreams replied.
ErrNoReply errors.Error = "no reply"
)
// ExchangeParallel returns the dirst successful response from one of u. It
// returns an error if all upstreams failed to exchange the request.
func ExchangeParallel(ups []Upstream, req *dns.Msg) (reply *dns.Msg, resolved Upstream, err error) {
upsNum := len(ups)
switch upsNum {
case 0:
return nil, nil, ErrNoUpstreams
case 1:
reply, err = exchangeAndLog(ups[0], req)
return reply, ups[0], err
default:
// Go on.
}
resCh := make(chan any, upsNum)
for _, f := range ups {
go exchangeAsync(f, req, resCh)
}
errs := []error{}
for range ups {
var r *ExchangeAllResult
r, err = receiveAsyncResult(resCh)
if err != nil {
if !errors.Is(err, ErrNoReply) {
errs = append(errs, err)
}
} else {
return r.Resp, r.Upstream, nil
}
}
// TODO(e.burkov): Probably it's better to return the joined error from
// each upstream that returned no response, and get rid of multiple
// [errors.Is] calls. This will change the behavior though.
if len(errs) == 0 {
return nil, nil, errors.Error("none of upstream servers responded")
}
return nil, nil, errors.Join(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 returns 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) {
upsNum := len(ups)
switch upsNum {
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, ErrNoReply
}
return []ExchangeAllResult{{Upstream: ups[0], Resp: reply}}, nil
default:
// Go on.
}
res = make([]ExchangeAllResult, 0, upsNum)
var errs []error
resCh := make(chan any, upsNum)
// Start exchanging concurrently.
for _, u := range ups {
go exchangeAsync(u, req, resCh)
}
// Wait for all exchanges to finish.
for range ups {
var r *ExchangeAllResult
r, err = receiveAsyncResult(resCh)
if err != nil {
errs = append(errs, err)
} else {
res = append(res, *r)
}
}
if len(errs) == upsNum {
return res, fmt.Errorf("all upstreams failed: %w", errors.Join(errs...))
}
return slices.Clip(res), nil
}
// receiveAsyncResult receives a single result from resCh or an error from
// errCh. It returns either a non-nil result or an error.
func receiveAsyncResult(resCh chan any) (res *ExchangeAllResult, err error) {
switch res := (<-resCh).(type) {
case error:
return nil, res
case *ExchangeAllResult:
if res.Resp == nil {
return nil, ErrNoReply
}
return res, nil
default:
return nil, fmt.Errorf("unexpected type %T of result", res)
}
}
// exchangeAsync tries to resolve DNS request with one upstream and sends the
// result to respCh.
func exchangeAsync(u Upstream, req *dns.Msg, resCh chan any) {
reply, err := exchangeAndLog(u, req)
if err != nil {
resCh <- err
} else {
resCh <- &ExchangeAllResult{Resp: reply, Upstream: u}
}
}
// 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)
dur := time.Since(start)
if len(req.Question) > 0 {
if q := &req.Question[0]; err == nil {
log.Debug("dnsproxy: upstream %s exchanged %s successfully in %s", addr, q, dur)
} else {
log.Debug("dnsproxy: upstream %s failed to exchange %s in %s: %s", addr, q, dur, err)
}
}
return reply, err
}
// LookupParallel tries to lookup for ip of host with all resolvers
// concurrently.
//
// Deprecated: Use [ParallelResolver] instead.
func LookupParallel(
ctx context.Context,
resolvers []Resolver,
host string,
) (addrs []netip.Addr, err error) {
return ParallelResolver(resolvers).LookupNetIP(ctx, "ip", host)
}