-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
60 lines (54 loc) · 1.55 KB
/
util.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
package transport
import (
"fmt"
"math/rand"
"net"
"strings"
)
func fullAddress(host string, defaultPort int) string {
if _, _, err := net.SplitHostPort(host); err == nil {
return host
}
idx := strings.Index(host, ":")
if idx >= 0 {
// IPv6 address detected
return fmt.Sprintf("[%v]:%v", host, defaultPort)
}
return fmt.Sprintf("%v:%v", host, defaultPort)
}
func dialWith(
dialer Dialer,
network, host string,
addresses []string,
port string,
) (c net.Conn, err error) {
switch len(addresses) {
case 0:
return nil, fmt.Errorf("no route to host %v", host)
case 1:
return dialer.Dial(network, net.JoinHostPort(addresses[0], port))
}
// Use randomization on DNS reported addresses combined with timeout and ACKs
// to spread potential load when starting up large number of beats using
// lumberjack.
//
// RFCs discussing reasons for ignoring order of DNS records:
// http://www.ietf.org/rfc/rfc3484.txt
// > is specific to locality-based address selection for multiple dns
// > records, but exists as prior art in "Choose some different ordering for
// > the dns records" done by a client
//
// https://tools.ietf.org/html/rfc1794
// > "Clients, of course, may reorder this information" - with respect to
// > handling order of dns records in a response.orwarded. Really required?
for _, i := range rand.Perm(len(addresses)) {
c, err = dialer.Dial(network, net.JoinHostPort(addresses[i], port))
if err == nil && c != nil {
return c, err
}
}
if err == nil {
err = fmt.Errorf("unable to connect to '%v'", host)
}
return nil, err
}