-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
107 lines (95 loc) · 2.18 KB
/
main.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
package main
import (
"log"
"os"
"time"
)
func main() {
log.SetOutput(os.Stdout)
log.SetFlags(log.Ltime | log.LUTC)
doWork := func(done <-chan interface{}, _ time.Duration) <-chan interface{} {
log.Println("Ward: Hello, I'm irresponsible!")
go func() {
<-done
log.Println("Ward: I am halting.")
}()
return nil
}
doWorkWithSteward := newSteward(4*time.Second, doWork)
done := make(chan interface{})
time.AfterFunc(9*time.Second, func() {
log.Println("main: halting steward and ward.")
close(done)
})
for range doWorkWithSteward(done, 4*time.Second) {
}
log.Println("Done.")
}
type startGoroutineFn func(
done <-chan interface{},
pulseInterval time.Duration,
) (heartbeat <-chan interface{})
var newSteward = func(
timeout time.Duration,
startGoroutine startGoroutineFn,
) startGoroutineFn {
return func(
done <-chan interface{},
pulseInterval time.Duration,
) <-chan interface{} { // 2
heartbeat := make(chan interface{})
go func() {
defer close(heartbeat)
var wardDone chan interface{}
var wardHeartbeat <-chan interface{}
startWard := func() { // 3
wardDone = make(chan interface{}) // 4
wardHeartbeat = startGoroutine(or(wardDone, done), timeout/2) //5
}
startWard()
pulse := time.Tick(pulseInterval)
monitorLoop:
for {
timeoutSignal := time.After(timeout)
for { // 6
select {
case <-pulse:
select {
case heartbeat <- struct{}{}:
default:
}
case <-wardHeartbeat:
continue monitorLoop
case <-timeoutSignal:
log.Println("steward: ward unhealthy; restarting")
close(wardDone)
startWard()
continue monitorLoop
case <-done:
return
}
}
}
}()
return heartbeat
}
}
// done 用于关闭 or goroutine
// channels 中的任意一个接收到信号后,都会通过 orDone 的关闭来转发
func or(done <-chan interface{}, channels ...<-chan interface{}) <-chan interface{} {
if len(channels) == 0 {
return done
}
//
orDone := make(chan interface{})
go func() {
defer close(orDone)
select {
case <-done:
case <-channels[0]:
case <-or(orDone, channels[1:]...):
}
}()
//
return orDone
}