-
Notifications
You must be signed in to change notification settings - Fork 1
/
pinger.go
68 lines (57 loc) · 1.09 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
package gerty
import (
"sync"
"time"
)
var interval = 30 * time.Second
type Monitoreable interface {
GetGroups() []Group
Failed(Monitor)
Restored(Monitor)
}
func Ping(subject Monitoreable) chan interface{} {
ticker := time.NewTicker(interval)
quit := make(chan interface{})
go func() {
for {
select {
case <-ticker.C:
refreshGroups(subject)
case <-quit:
ticker.Stop()
return
}
}
}()
refreshGroups(subject)
return quit
}
func refreshGroups(subject Monitoreable) {
groups := subject.GetGroups()
for i := range groups {
refresh(groups[i].Monitors, subject)
}
}
func check(m Monitor, wg *sync.WaitGroup) {
defer wg.Done()
m.Check()
}
func refresh(monitors []Monitor, subject Monitoreable) {
var wg sync.WaitGroup
wg.Add(len(monitors))
for i := range monitors {
go func(i int) {
monitor := monitors[i]
check(monitor, &wg)
if AllFailed(monitor) && !monitor.IsTripped() {
monitor.Trip()
go subject.Failed(monitor)
}
if AllOk(monitor) && monitor.IsTripped() {
monitor.Untrip()
go subject.Restored(monitor)
}
}(i)
}
wg.Wait()
}