-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimingwheel.go
140 lines (119 loc) · 2.65 KB
/
timingwheel.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package timer
import (
"errors"
"log"
"sync"
"time"
)
const (
LevelCnt = 7
)
var (
ErrWheelLevelInvalid = errors.New("timing wheel level invalid, should between [1, 7]")
ErrSlotInvalid = errors.New("slot index invalid")
ErrTickDurInvalid = errors.New("tick duration invalid")
ErrDelayOverflow = errors.New("time delay overflow")
)
type TimingWheel struct {
sync.Mutex
levelCnt int
tickDur time.Duration
wheels []*levelWheel
exit chan struct{}
}
func NewTimingWheel(tickDur time.Duration) (*TimingWheel, error) {
if tickDur <= 0*time.Second {
return nil, ErrTickDurInvalid
}
tw := &TimingWheel{
levelCnt: LevelCnt,
tickDur: tickDur,
wheels: make([]*levelWheel, LevelCnt),
exit: make(chan struct{}),
}
for i := 0; i < LevelCnt; i++ {
tw.wheels[i] = newLevelWheel()
}
return tw, nil
}
func (tw *TimingWheel) AddFunc(delay time.Duration, f func()) error {
tw.Lock()
defer tw.Unlock()
tc := uint64(delay / tw.tickDur)
if tc > (1 << (uint(tw.levelCnt) * 8)) {
return ErrDelayOverflow
}
var level int
var slotIdx int
levelSlotIdx := make([]uint8, tw.levelCnt)
for i := 0; i < tw.levelCnt-1; i++ {
levelSlotIdx[i] = tw.wheels[i].curPos + uint8(tc&0xff)
tc = tc >> 8
}
job := &Job{
levelSlotIdx: levelSlotIdx,
f: f,
CreateTime: time.Now(),
}
for i := tw.levelCnt - 1; i >= 0; i-- {
if levelSlotIdx[i] != 0 {
level = i
slotIdx = int(levelSlotIdx[i])
break
}
}
// log.Printf("levelSlotIdx:tc:%d %+v", uint64(delay/tw.tickDur), levelSlotIdx)
return tw.addJob(level, slotIdx, job)
}
func (tw *TimingWheel) addJob(level, slotIdx int, job *Job) error {
if level < 0 || level >= len(tw.wheels) {
return ErrWheelLevelInvalid
}
if slotIdx < 0 || slotIdx >= SlotCnt {
return ErrSlotInvalid
}
tw.wheels[level].addJob(slotIdx, job)
return nil
}
func (tw *TimingWheel) Run() {
ticker := time.NewTicker(tw.tickDur) // time.Tick(tw.tickDur)
for {
select {
case <-ticker.C:
tw.tick()
case <-tw.exit:
ticker.Stop()
log.Println("timingwheel exit")
return
}
}
}
func (tw *TimingWheel) tick() {
tw.Lock()
defer tw.Unlock()
// move position
tw.wheels[0].curPos++
// fire events
tw.wheels[0].fireEvents()
if tw.wheels[0].curPos != 0 {
return
}
// turn wheels
for i := 1; i < tw.levelCnt; i++ {
tw.wheels[i].curPos++
// move list to lower level
l := tw.wheels[i].slots[tw.wheels[i].curPos]
for e := l.Front(); e != nil; e = e.Next() {
job := e.Value.(*Job)
slotIdx := int(job.levelSlotIdx[i-1])
tw.addJob(i-1, slotIdx, job)
}
l.Init()
if tw.wheels[i].curPos != 0 {
break
}
}
}
func (tw *TimingWheel) Close() {
tw.exit <- struct{}{}
}