-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpinger.go
162 lines (135 loc) Β· 2.99 KB
/
pinger.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
package pinger
import (
"context"
"net"
"strings"
"sync"
"time"
)
var (
defaultStatsBuf = 60
zeroDur = time.Duration(0)
)
// PingStat struct is used to record the ping result.
type PingStat struct {
Host string
PktSent int
PktLossRate float64
Mean time.Duration
Last time.Duration
Best time.Duration
Worst time.Duration
}
type destination struct {
host string
remote *net.IPAddr
fail int
*history
}
type history struct {
received int
lost int
results []time.Duration // ring, start index = .received%len
mtx sync.RWMutex
}
func (h *history) addResult(rtt time.Duration, err error) {
h.mtx.Lock()
if err != nil {
h.lost++
} else {
h.results[h.received%len(h.results)] = rtt
h.received++
}
h.mtx.Unlock()
}
func (h *history) compute() (st PingStat) {
h.mtx.RLock()
defer h.mtx.RUnlock()
if h.received == 0 {
if h.lost > 0 {
st.PktLossRate = 1.0
}
return
}
collection := h.results[:]
st.PktSent = h.received + h.lost
size := len(h.results)
st.Last = collection[(h.received-1)%size]
// we don't yet have filled the buffer
if h.received <= size {
collection = h.results[:h.received]
size = h.received
}
st.PktLossRate = float64(h.lost) / float64(h.received+h.lost)
st.Best, st.Worst = collection[0], collection[0]
total := time.Duration(0)
for _, rtt := range collection {
if rtt < st.Best {
st.Best = rtt
}
if rtt > st.Worst {
st.Worst = rtt
}
total += rtt
}
st.Mean = time.Duration(float64(total) / float64(size))
return
}
func resolve(addr string, timeout time.Duration) ([]net.IPAddr, error) {
if strings.ContainsRune(addr, '%') {
ipaddr, err := net.ResolveIPAddr("ip", addr)
if err != nil {
return nil, err
}
return []net.IPAddr{*ipaddr}, nil
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return net.DefaultResolver.LookupIPAddr(ctx, addr)
}
func sortHosts(stats map[string]PingStat, hosts ...string) []PingStat {
ordered := make([]PingStat, len(hosts))
for i, host := range hosts {
ordered[i] = stats[host]
}
return ordered
}
type calcStatsReq struct {
maxConcurrency int
failover int
pingCount int
dest []*destination
ping func(d *destination, args ...interface{}) error
setInterval func() time.Duration
args interface{}
}
func calculateStats(csr calcStatsReq) map[string]PingStat {
stats := make(map[string]PingStat)
mux := sync.Mutex{}
wg := sync.WaitGroup{}
sema := make(chan struct{}, csr.maxConcurrency)
for _, dest := range csr.dest {
wg.Add(1)
go func(d *destination) {
defer wg.Done()
for c := 0; c < csr.pingCount; c++ {
sema <- struct{}{}
if err := csr.ping(d, csr.args); err != nil {
d.fail++
}
mux.Lock()
stat := d.compute()
stat.Host = d.host
stats[d.host] = stat
mux.Unlock()
<-sema
if d.fail >= csr.failover {
return
}
time.Sleep(csr.setInterval())
}
}(dest)
}
wg.Wait()
return stats
}