/
WatchDog.go
67 lines (53 loc) · 1.1 KB
/
WatchDog.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
package WatchDog
import (
"sync/atomic"
"time"
"github.com/6xiao/go/Common"
)
type WatchDog struct {
wait time.Duration
hung func()
meat int32
count int32
}
func NewDog(duration time.Duration, meat int32, hung func()) *WatchDog {
d := new(WatchDog)
d.wait = duration
d.hung = hung
d.meat = meat
go d.eat()
return d
}
func (this *WatchDog) eat() {
defer Common.CheckPanic()
for this.hung != nil && atomic.LoadInt32(&this.count) < 3 {
time.Sleep(this.wait)
m := atomic.LoadInt32(&this.meat)
if m < 0 {
return
}
if m == 0 {
this.hung()
atomic.AddInt32(&this.count, 1)
} else {
atomic.StoreInt32(&this.meat, m/2)
atomic.StoreInt32(&this.count, 0)
}
}
}
func (this *WatchDog) Feed(meat uint16) bool {
defer Common.CheckPanic()
// meat enought
if atomic.LoadInt32(&this.meat) > 1024*65536 {
return true
}
return atomic.AddInt32(&this.meat, int32(meat)) > 0
}
func (this *WatchDog) Kill() {
defer Common.CheckPanic()
atomic.StoreInt32(&this.meat, -65536)
}
func (this *WatchDog) Living() bool {
defer Common.CheckPanic()
return atomic.LoadInt32(&this.count) == 0
}