/
balancer.go
65 lines (54 loc) · 1.44 KB
/
balancer.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
package v2
import (
"fmt"
"math/rand"
"net"
)
// Balancer provides IPs resolved from a DNS address in random order
type Balancer struct {
addr string
randSource func() int
lookup func(string) ([]net.IP, error)
}
// BalancerOption is a type that will manipulate a config
type BalancerOption func(*Balancer)
// WithLookup sets the behavior of looking up IPs
func WithLookup(lookup func(string) ([]net.IP, error)) func(*Balancer) {
return func(b *Balancer) {
b.lookup = lookup
}
}
func WithRandSource(rand func() int) func(*Balancer) {
return func(b *Balancer) {
b.randSource = rand
}
}
// NewBalancer returns a Balancer
func NewBalancer(addr string, opts ...BalancerOption) *Balancer {
balancer := &Balancer{
addr: addr,
lookup: net.LookupIP,
randSource: rand.Int, //nolint:gosec
}
for _, o := range opts {
o(balancer)
}
return balancer
}
// NextHostPort returns hostport resolved from the balancer's addr.
// It returns error for an invalid addr or if lookup failed or
// doesn't resolve to anything.
func (b *Balancer) NextHostPort() (string, error) {
host, port, err := net.SplitHostPort(b.addr)
if err != nil {
return "", err
}
ips, err := b.lookup(host)
if err != nil {
return "", err
}
if len(ips) == 0 {
return "", fmt.Errorf("lookup failed with addr %s", b.addr)
}
return net.JoinHostPort(ips[b.randSource()%len(ips)].String(), port), nil //nolint:gosec // randomly pick an IP not a security issue
}