-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheel_timer.go
83 lines (70 loc) · 1.64 KB
/
wheel_timer.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
package timer
import (
"fmt"
"time"
"github.com/jursonmo/timer/ilist"
)
const (
Stoped = 0
NotReady = 1
Ready = 2
Running = 3
InPool = 4
FromPool = InPool
)
/* addtimer release Get()
//init(Stoped)----------->NotReady ---> Ready --->Running ---------->InPool------->Stoped
|
<--------------|
deltimer
*/
//目前只有stoped和NotReady的状态timer.Stop()才返回true, 这样才能ResetTimer
/*
if t.Stop() {
t.ResetTimer(d, period)
}
*/
/*
```go
1.
if t.Stop() {
t.Release() //timer 已经Stop,可以Release
}
2.
callback = func(time.Time, args ...interface{}) {
t.Release() //timer 的callback已经执行,可以Release
}
```
*/
// todo:按道理timer excute完后可以Stop() 和 Reset(); 同时timer in sync.Pool 是不能做任何操作的
type WheelTimer = timer
type timer struct {
ilist.Entry
list *ilist.List
w *Wheel
expires uint64
period uint64
state int
f func(time.Time, ...interface{})
arg []interface{}
}
func Timers() int {
//return defaultWheel.Timers()
return defaultWheelShard.Timers()
}
func NewWheelTimerFunc(d time.Duration, f func(time.Time, ...interface{}), arg ...interface{}) *WheelTimer {
//return defaultWheel.NewWheelTimerFunc(d, f, arg...)
return defaultWheelShard.NewWheelTimerFunc(d, f, arg...)
}
func (t *timer) Stop() bool {
return t.w.delTimer(t)
}
func (t *timer) ResetTimer(d time.Duration, period time.Duration) bool {
return t.w.resetTimer(t, d, period)
}
func (t *timer) Release() {
t.w.releaseTimer(t)
}
func (t *timer) Info() string {
return fmt.Sprintf("expires:%d, period:%d, args:%v", t.expires, t.period, t.arg)
}