-
Notifications
You must be signed in to change notification settings - Fork 0
/
ips.go
65 lines (55 loc) · 973 Bytes
/
ips.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 resolver
import (
"net"
"sync"
"sync/atomic"
)
// ips ...
type ips struct {
mu sync.RWMutex
ipIdx uint64
ipList []net.IP
}
// newIps ...
func newIps() *ips {
return &ips{
ipList: make([]net.IP, 0),
}
}
func newIpsFromList(listIP []string) *ips {
result := make([]net.IP, 0, len(listIP))
for _, v := range listIP {
ip := net.ParseIP(v)
if ip != nil {
result = append(result, ip)
}
}
return &ips{
ipList: result,
}
}
// setIpList ...
func (i *ips) setIpList(ipList []net.IP) {
i.mu.Lock()
defer i.mu.Unlock()
i.ipList = ipList
}
// getNextIPWithIndex ...
func (i *ips) getNextIPWithIndex() (net.IP, int) {
i.mu.RLock()
if len(i.ipList) == 0 {
i.mu.RUnlock()
return nil, 0
}
idx := i.ipIdx % uint64(len(i.ipList))
ipRet := i.ipList[idx]
i.mu.RUnlock()
atomic.AddUint64(&i.ipIdx, 1)
return ipRet, int(idx)
}
// getList ...
func (i *ips) getList() []net.IP {
i.mu.RLock()
defer i.mu.RUnlock()
return i.ipList
}