-
Notifications
You must be signed in to change notification settings - Fork 19
/
wait_quit.go
executable file
·50 lines (42 loc) · 1.04 KB
/
wait_quit.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
/*
封装一个类用于等待类退出,分为两步:
1、类处理完退出操作后调用AllDone()
2、类的Quit()方法中调用Quit()
*/
package lib
import (
"time"
"logd/loglib"
)
type WaitQuit struct {
name string
allowTimeout int //允许的超时次数, 小于0表示不限次数
nTimeout int //已超时次数
ch chan bool
}
func NewWaitQuit(modName string, allow ...int) *WaitQuit {
ch := make(chan bool)
a := 2
if len(allow) > 0 {
a = allow[0]
}
return &WaitQuit{modName, a, 0, ch}
}
func (this *WaitQuit) AllDone() {
this.ch <- true
}
func (this *WaitQuit) Quit() bool {
ret := false
select {
case <- this.ch:
loglib.Info(this.name + " safe quit.")
ret = true
case <- time.After(2 * time.Second):
loglib.Info(this.name + " quit timeout")
this.nTimeout++
if this.allowTimeout > 0 && this.nTimeout >= this.allowTimeout {
ret = true
}
}
return ret
}