-
Notifications
You must be signed in to change notification settings - Fork 5
/
gtimer_loop.go
51 lines (49 loc) · 1.12 KB
/
gtimer_loop.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
package timeTick
import (
"time"
"timeWheel/gtype"
)
// 执行时间轮刻度逻辑
func (w *wheel) proceed() {
n := w.ticks.Add(1)
l := w.slots[int(n%w.number)]
length := l.Len()
if length > 0 {
go func(l *gtype.List, nowTicks int64) {
entry := (*Entry)(nil)
nowMs := time.Now().UnixNano() / 1e6
for i := length; i > 0; i-- {
if v := l.PopFront(); v == nil {
break
} else {
entry = v.(*Entry)
}
// 是否满足运行条件
runnable, addable := entry.check(nowTicks, nowMs)
if runnable {
//异步执行运行
go func(entry *Entry) {
defer func() {
if err := recover(); err != nil {
if err != gPANIC_EXIT {
panic(err)
} else {
entry.Close()
}
}
if entry.Status() == STATUS_RUNNING { //todo:entry将running改为ready?
entry.SetStatus(STATUS_READY)
}
}()
entry.job()
}(entry)
}
// 是否继续添运行,滚动任务
if addable {
//优先从chird time wheel开始添加
entry.wheel.timer.doAddEntryByParent(entry.rawIntervalMs, entry)
}
}
}(l, n)
}
}