-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathresolver.go
201 lines (167 loc) · 4.37 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
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"net"
"os"
"strings"
"sync"
"sync/atomic"
"time"
)
type IPResolvedCallback = func(hostname string, newIPs []string, removedIPs []string)
type addressWithCallback struct {
addrs map[string]time.Time
addrExpire time.Duration
callback IPResolvedCallback
}
// Resolver dynamically resolve the host name to IP addresses
type Resolver struct {
sync.Mutex
//resolve interval
interval time.Duration
// 0: no stop, 1: stop the resolve
stop int32
hostIPs map[string]*addressWithCallback
}
var ADDRESS_EXPIRE time.Duration = time.Duration(60)
func init() {
expire := os.Getenv("ADDRESS_EXPIRE")
if len(expire) <= 0 {
expire = "60s"
}
d, err := time.ParseDuration(expire)
if err != nil {
d = time.Duration(60)
}
ADDRESS_EXPIRE = d
}
func newAddressWithCallback(callback IPResolvedCallback, addrExpire time.Duration) *addressWithCallback {
return &addressWithCallback{addrs: make(map[string]time.Time),
addrExpire: addrExpire,
callback: callback}
}
func (ac *addressWithCallback) addAddrs(addrs []string) {
for _, addr := range addrs {
ac.addrs[addr] = time.Now().Add(ac.addrExpire)
}
}
func (ac *addressWithCallback) cleanExpiredAddrs() []string {
expiredAddrs := make([]string, 0)
for addr, expireInfo := range ac.addrs {
if expireInfo.Before(time.Now()) {
expiredAddrs = append(expiredAddrs, addr)
}
}
for _, addr := range expiredAddrs {
delete(ac.addrs, addr)
}
return expiredAddrs
}
func (ac *addressWithCallback) getAddrs() []string {
addrs := make([]string, 0)
for addr := range ac.addrs {
addrs = append(addrs, addr)
}
return addrs
}
func NewResolver(interval int) *Resolver {
r := &Resolver{interval: time.Duration(interval) * time.Second,
stop: 0,
hostIPs: make(map[string]*addressWithCallback)}
go r.periodicalResolve()
return r
}
func (r *Resolver) ResolveHost(addr string, callback IPResolvedCallback) {
r.Lock()
defer r.Unlock()
if _, ok := r.hostIPs[addr]; !ok {
r.hostIPs[addr] = newAddressWithCallback(callback, ADDRESS_EXPIRE)
ips, err := r.doResolve(addr)
if err == nil {
r.hostIPs[addr].addAddrs(ips)
callback(addr, ips, make([]string, 0))
}
}
}
func (r *Resolver) StopResolve(hostname string) {
r.Lock()
defer r.Unlock()
if _, ok := r.hostIPs[hostname]; ok {
delete(r.hostIPs, hostname)
}
}
func (r *Resolver) 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 *Resolver) Stop() {
if atomic.CompareAndSwapInt32(&r.stop, 0, 1) {
log.Info("stop the hostname to IP resolve")
}
}
func (r *Resolver) isStopped() bool {
return atomic.LoadInt32(&r.stop) != 0
}
func (r *Resolver) GetAddrsOfHost(hostname string) []string {
r.Lock()
defer r.Unlock()
if v, ok := r.hostIPs[hostname]; ok {
return v.getAddrs()
}
return make([]string, 0)
}
func (r *Resolver) periodicalResolve() {
for !r.isStopped() {
hostnames := r.getHostnames()
for _, hostname := range hostnames {
addrs, err := r.doResolve(hostname)
if err != nil {
log.WithFields(log.Fields{"hostname": hostname}).Error("Fail to resolve host to IP")
}
r.addressResolved(hostname, addrs, err)
}
time.Sleep(r.interval)
}
}
func (r *Resolver) addressResolved(hostname string, addrs []string, err error) {
r.Lock()
defer r.Unlock()
if entry, ok := r.hostIPs[hostname]; ok {
if err == nil {
oldAddrs := entry.getAddrs()
entry.addAddrs(addrs)
newAddrs := strArraySub(addrs, oldAddrs)
removedAddrs := entry.cleanExpiredAddrs()
if len(newAddrs) > 0 || len(removedAddrs) > 0 {
log.WithFields(log.Fields{"hostname": hostname, "newAddrs": strings.Join(newAddrs, ","), "removedAddrs": strings.Join(removedAddrs, ",")}).Info("the ip address of host is changed")
go entry.callback(hostname, newAddrs, removedAddrs)
}
}
}
}
func (r *Resolver) doResolve(addr string) ([]string, error) {
hostname, port, err := splitAddr(addr)
if err != nil {
return nil, err
}
ips, err := net.LookupIP(hostname)
if err != nil {
return nil, err
}
result := make([]string, 0)
for _, ip := range ips {
s := ip.String()
if strings.Index(s, ":") != -1 {
s = fmt.Sprintf("[%s]", s)
}
result = append(result, fmt.Sprintf("%s:%s", s, port))
}
return result, nil
}