-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresolver.go
190 lines (159 loc) · 4.47 KB
/
resolver.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
package main
import (
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"go.uber.org/zap"
)
type PreConfigHostResolver struct {
// Name to IP mapping
hostIPs map[string]string
}
func NewPreConfigHostResolver() *PreConfigHostResolver {
return &PreConfigHostResolver{hostIPs: make(map[string]string)}
}
func (hr *PreConfigHostResolver) AddHostIP(name string, ip string) {
hr.hostIPs[name] = ip
}
// GetIp get the IP by hostname
func (hr *PreConfigHostResolver) GetIp(name string) (string, error) {
if net.ParseIP(name) != nil {
return name, nil
}
if ip, ok := hr.hostIPs[name]; ok {
return ip, nil
}
ips, err := net.LookupIP(name)
if err == nil && len(ips) > 0 {
return ips[0].String(), nil
}
return "", fmt.Errorf("fail to find IP of %s", name)
}
type IPResolvedCallback = func(hostname string, newIPs []string, removedIPs []string)
type addressWithCallback struct {
addrs []string
//times of resolved
failed int
callback IPResolvedCallback
}
// DynamicHostResolver dynamically resolve the host name to IP addresses
type DynamicHostResolver struct {
sync.Mutex
//resolve interval
interval time.Duration
// 0: no stop, 1: stop the resolve
stop int32
hostIPs map[string]*addressWithCallback
}
func NewDynamicHostResolver(interval int) *DynamicHostResolver {
r := &DynamicHostResolver{interval: time.Duration(interval) * time.Second,
stop: 0,
hostIPs: make(map[string]*addressWithCallback)}
go r.periodicalResolve()
return r
}
func (r *DynamicHostResolver) ResolveHost(addr string, callback IPResolvedCallback) {
r.Lock()
defer r.Unlock()
if _, ok := r.hostIPs[addr]; !ok {
ips, err := r.doResolve(addr)
if err == nil {
r.hostIPs[addr] = &addressWithCallback{addrs: ips, failed: 0, callback: callback}
callback(addr, ips, make([]string, 0))
} else {
r.hostIPs[addr] = &addressWithCallback{addrs: make([]string, 0), failed: 0, callback: callback}
}
}
}
/*
func (r *DynamicHostResolver) StopResolve(hostname string) {
r.Lock()
defer r.Unlock()
if _, ok := r.hostIPs[hostname]; ok {
delete(r.hostIPs, hostname)
}
}*/
func (r *DynamicHostResolver) getHostnames() []string {
r.Lock()
defer r.Unlock()
hostnames := make([]string, 0)
for hostname := range r.hostIPs {
hostnames = append(hostnames, hostname)
}
return hostnames
}
// Stop stop the hostname resolve
func (r *DynamicHostResolver) Stop() {
if atomic.CompareAndSwapInt32(&r.stop, 0, 1) {
zap.L().Info("stop the hostname to IP resolve")
}
}
func (r *DynamicHostResolver) isStopped() bool {
return atomic.LoadInt32(&r.stop) != 0
}
func (r *DynamicHostResolver) GetAddrsOfHost(hostname string) []string {
result := make([]string, 0)
r.Lock()
defer r.Unlock()
if v, ok := r.hostIPs[hostname]; ok {
result = append(result, v.addrs...)
}
return result
}
func (r *DynamicHostResolver) periodicalResolve() {
for !r.isStopped() {
hostnames := r.getHostnames()
for _, hostname := range hostnames {
addrs, err := r.doResolve(hostname)
if err != nil {
zap.L().Error("Fail to resolve host to IP", zap.String("hostname", hostname))
}
r.addressResolved(hostname, addrs, err)
}
time.Sleep(r.interval)
}
}
func (r *DynamicHostResolver) addressResolved(hostname string, addrs []string, err error) {
r.Lock()
defer r.Unlock()
if entry, ok := r.hostIPs[hostname]; ok {
if err != nil {
entry.failed += 1
if entry.failed > 3 && len(entry.addrs) > 0 {
newAddrs := make([]string, 0)
removedAddrs := entry.addrs
entry.addrs = newAddrs
zap.L().Error("the failed times for resolving hostname exceeds 3", zap.String("hostname", hostname), zap.Int("failed", entry.failed))
entry.failed = 0
go entry.callback(hostname, newAddrs, removedAddrs)
}
} else {
newAddrs := strArraySub(addrs, entry.addrs)
removedAddrs := strArraySub(entry.addrs, addrs)
entry.failed = 0
entry.addrs = addrs
if len(newAddrs) > 0 || len(removedAddrs) > 0 {
zap.L().Info("the ip address of host is changed", zap.String("hostname", hostname), zap.String("newAddrs", strings.Join(newAddrs, ",")), zap.String("removedAddrs", strings.Join(removedAddrs, ",")))
go entry.callback(hostname, newAddrs, removedAddrs)
}
}
}
}
func (r *DynamicHostResolver) doResolve(hostname string) ([]string, error) {
ips, err := net.LookupIP(hostname)
if err != nil {
return nil, err
}
result := make([]string, 0)
for _, ip := range ips {
s := ip.String()
if strings.Contains(s, ":") {
s = fmt.Sprintf("[%s]", s)
}
result = append(result, s)
}
return result, nil
}