-
Notifications
You must be signed in to change notification settings - Fork 12
/
service_rrdns.go
64 lines (58 loc) · 1.46 KB
/
service_rrdns.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
package cslb
import (
"net"
"sync/atomic"
"unsafe"
)
type rrDNSService struct {
ipv4 bool
ipv6 bool
hostnames []string
nodes unsafe.Pointer // Pointer to []*net.IPAddr
}
// NewRRDNSService is for Round-robin DNS load balancing solution.
// Usually multiple A or AAAA records are associated with single hostname.
// Node type: *net.IPAddr
//
// For example:
// Hostname www.a.com
// |- A 1.2.3.4
// |- A 2.3.4.5
// |- A 3.4.5.6
// ...
// Everytime a client wants to send a request, one of these A records will be chosen to establish connection.
func NewRRDNSService(hostnames []string, ipv4 bool, ipv6 bool) *rrDNSService {
return &rrDNSService{
ipv4: ipv4,
ipv6: ipv6,
hostnames: hostnames,
nodes: nil,
}
}
func (s *rrDNSService) Nodes() []Node {
nodes := (*[]*net.IPAddr)(atomic.LoadPointer(&s.nodes))
result := make([]Node, 0, len(*nodes))
for _, n := range *nodes {
result = append(result, n)
}
return result
}
func (s *rrDNSService) NodeFailedCallbackFunc() func(node Node) {
return nil
}
func (s *rrDNSService) Refresh() {
ips := make([]*net.IPAddr, 0, len(s.hostnames))
for _, h := range s.hostnames {
if results, err := net.LookupIP(h); err == nil {
for _, ip := range results {
switch {
case ip.To4() != nil && s.ipv4:
fallthrough
case ip.To16() != nil && s.ipv6:
ips = append(ips, &net.IPAddr{IP: ip})
}
}
}
}
atomic.StorePointer(&s.nodes, (unsafe.Pointer)(&ips))
}