-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdialer.go
349 lines (314 loc) · 9.42 KB
/
dialer.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package ara
import (
"context"
"errors"
"net"
"syscall"
"time"
)
type simpleAddr struct {
addr string
network string
}
func (addr simpleAddr) String() string {
return addr.addr
}
func (addr simpleAddr) Network() string {
return addr.network
}
type timeoutErr struct{}
func (timeoutErr) Error() string {
return "timeout"
}
func (timeoutErr) Timeout() bool {
return true
}
func (timeoutErr) Temporary() bool {
return false
}
var errMissingAddress = errors.New("missing address")
// Dialer is a partial replacement for net.Dialer but it still
// uses net.Dialer internally.
//
// Unlike net.Dialer, it accepts an actual customizable Resolver.
//
// A Dialer contains options for connecting to an address.
//
// The zero value for each field is equivalent to dialing
// without that option.
type Dialer struct {
// Timeout is the maximum amount of time a dial will wait for
// a connect to complete. If Deadline is also set, it may fail
// earlier.
//
// The default is no timeout.
//
// When using TCP and dialing a host name with multiple IP
// addresses, the timeout may be divided between them.
//
// With or without a timeout, the operating system may impose
// its own earlier timeout. For instance, TCP timeouts are
// often around 3 minutes.
Timeout time.Duration
// Deadline is the absolute point in time after which dials
// will fail. If Timeout is set, it may fail earlier.
// Zero means no deadline, or dependent on the operating system
// as with the Timeout option.
Deadline time.Time
// LocalAddr is the local address to use when dialing an
// address. The address must be of a compatible type for the
// network being dialed.
// If nil, a local address is automatically chosen.
LocalAddr net.Addr
// FallbackDelay specifies the length of time to wait before
// spawning a RFC 6555 Fast Fallback connection. That is, this
// is the amount of time to wait for IPv6 to succeed before
// assuming that IPv6 is misconfigured and falling back to
// IPv4.
//
// If zero, a default delay of 300ms is used.
// A negative value disables Fast Fallback support.
FallbackDelay time.Duration
// KeepAlive specifies the interval between keep-alive
// probes for an active network connection.
// If zero, keep-alive probes are sent with a default value
// (currently 15 seconds), if supported by the protocol and operating
// system. Network protocols or operating systems that do
// not support keep-alives ignore this field.
// If negative, keep-alive probes are disabled.
KeepAlive time.Duration
// Resolver optionally specifies an alternate resolver to use.
Resolver Resolver
// If Control is not nil, it is called after creating the network
// connection but before actually dialing.
//
// Network and address parameters passed to Control method are not
// necessarily the ones passed to Dial. For example, passing "tcp" to Dial
// will cause the Control function to be called with "tcp4" or "tcp6".
Control func(network, address string, c syscall.RawConn) error
// Underlying dialer
d *net.Dialer
}
// DialContext connects to the address on the named network using
// the provided context.
//
// The provided Context must be non-nil. If the context expires before
// the connection is complete, an error is returned. Once successfully
// connected, any expiration of the context will not affect the
// connection.
//
// When using TCP, and the host in the address parameter resolves to multiple
// network addresses, any dial timeout (from d.Timeout or ctx) is spread
// over each consecutive dial, such that each is given an appropriate
// fraction of the time to connect.
// For example, if a host has 4 IP addresses and the timeout is 1 minute,
// the connect to each single address will be given 15 seconds to complete
// before trying the next one.
//
// See func net.Dial for a description of the network and address
// parameters.
func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
var addresses []string
ip := net.ParseIP(host)
if ip != nil {
addresses = []string{address}
} else {
addresses, err = d.resolver().LookupHost(ctx, host)
if err != nil {
return nil, err
}
for i, address := range addresses {
addresses[i] = net.JoinHostPort(address, port)
}
}
var primaries, fallbacks []string
if d.dualStack() && network == "tcp" {
primaries, fallbacks = partition(addresses)
} else {
primaries = addresses
}
var c net.Conn
if len(fallbacks) > 0 {
c, err = d.dialParallel(ctx, network, primaries, fallbacks)
} else {
c, err = d.dialSerial(ctx, network, primaries)
}
return c, err
}
func (d *Dialer) resolver() Resolver {
if d.Resolver != nil {
return d.Resolver
}
return net.DefaultResolver
}
func (d *Dialer) dualStack() bool {
return d.FallbackDelay >= 0
}
func (d *Dialer) dialSerial(ctx context.Context, network string, addresses []string) (net.Conn, error) {
var firstErr error
for i, addr := range addresses {
saddr := simpleAddr{addr: addr, network: network}
select {
case <-ctx.Done():
return nil, &net.OpError{Op: "dial", Net: network, Source: d.LocalAddr, Addr: saddr, Err: ctx.Err()}
default:
}
deadline, _ := ctx.Deadline()
partialDeadline, err := partialDeadline(time.Now(), deadline, len(addresses)-i)
if err != nil {
// Ran out of time.
if firstErr == nil {
firstErr = &net.OpError{Op: "dial", Net: network, Source: d.LocalAddr, Addr: saddr, Err: err}
}
break
}
dialCtx := ctx
if partialDeadline.Before(deadline) {
var cancel context.CancelFunc
dialCtx, cancel = context.WithDeadline(ctx, partialDeadline)
defer cancel()
}
c, err := d.dialer().DialContext(dialCtx, network, addr)
if err == nil {
return c, nil
}
if firstErr == nil {
firstErr = err
}
}
if firstErr == nil {
firstErr = &net.OpError{Op: "dial", Net: network, Source: nil, Addr: nil, Err: errMissingAddress}
}
return nil, firstErr
}
// dialParallel races two copies of dialSerial, giving the first a
// head start. It returns the first established connection and
// closes the others. Otherwise it returns an error from the first
// primary address.
func (d *Dialer) dialParallel(ctx context.Context, network string, primaries, fallbacks []string) (net.Conn, error) {
returned := make(chan struct{})
defer close(returned)
type dialResult struct {
net.Conn
error
primary bool
done bool
}
results := make(chan dialResult) // unbuffered
startRacer := func(ctx context.Context, primary bool) {
ras := primaries
if !primary {
ras = fallbacks
}
c, err := d.dialSerial(ctx, network, ras)
select {
case results <- dialResult{Conn: c, error: err, primary: primary, done: true}:
case <-returned:
if c != nil {
_ = c.Close()
}
}
}
var primary, fallback dialResult
// Start the main racer.
primaryCtx, primaryCancel := context.WithCancel(ctx)
defer primaryCancel()
go startRacer(primaryCtx, true)
// Start the timer for the fallback racer.
fallbackTimer := time.NewTimer(d.fallbackDelay())
defer fallbackTimer.Stop()
for {
select {
case <-fallbackTimer.C:
fallbackCtx, fallbackCancel := context.WithCancel(ctx)
defer fallbackCancel()
go startRacer(fallbackCtx, false)
case res := <-results:
if res.error == nil {
return res.Conn, nil
}
if res.primary {
primary = res
} else {
fallback = res
}
if primary.done && fallback.done {
return nil, primary.error
}
if res.primary && fallbackTimer.Stop() {
// If we were able to stop the timer, that means it
// was running (hadn't yet started the fallback), but
// we just got an error on the primary path, so start
// the fallback immediately (in 0 nanoseconds).
fallbackTimer.Reset(0)
}
}
}
}
func (d *Dialer) fallbackDelay() time.Duration {
if d.FallbackDelay > 0 {
return d.FallbackDelay
} else {
return 300 * time.Millisecond
}
}
func (d *Dialer) dialer() *net.Dialer {
if d.d != nil {
return d.d
}
d.d = &net.Dialer{
Timeout: d.Timeout,
Deadline: d.Deadline,
LocalAddr: d.LocalAddr,
FallbackDelay: d.FallbackDelay,
KeepAlive: d.KeepAlive,
Control: d.Control,
}
return d.d
}
// partialDeadline returns the deadline to use for a single address,
// when multiple addresses are pending.
func partialDeadline(now, deadline time.Time, addrsRemaining int) (time.Time, error) {
if deadline.IsZero() {
return deadline, nil
}
timeRemaining := deadline.Sub(now)
if timeRemaining <= 0 {
return time.Time{}, timeoutErr{}
}
// Tentatively allocate equal time to each remaining address.
timeout := timeRemaining / time.Duration(addrsRemaining)
// If the time per address is too short, steal from the end of the list.
const saneMinimum = 2 * time.Second
if timeout < saneMinimum {
if timeRemaining < saneMinimum {
timeout = timeRemaining
} else {
timeout = saneMinimum
}
}
return now.Add(timeout), nil
}
// partition divides given address for dualstack usage
func partition(addresses []string) (primaries []string, fallbacks []string) {
var primaryLabel bool
for i, addr := range addresses {
label := isIPv4(addr)
if i == 0 || label == primaryLabel {
primaryLabel = label
primaries = append(primaries, addr)
} else {
fallbacks = append(fallbacks, addr)
}
}
return
}
// isIPv4 reports whether addr contains an IPv4 address.
func isIPv4(addr string) bool {
tcpAddr, err := net.ResolveTCPAddr("tcp", addr)
return err == nil && tcpAddr.IP.To4() != nil
}