-
Notifications
You must be signed in to change notification settings - Fork 1
/
timewheel.go
195 lines (177 loc) · 3.94 KB
/
timewheel.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package timewheel
import (
"container/list"
"errors"
"fmt"
"sync"
"time"
)
type TimeWheel struct {
interval time.Duration
ticker *time.Ticker
slots []*list.List
currentPos int
slotNum int
addTaskChan chan *task // 添加任务管道
stopChan chan bool // 停止轮盘管道
taskRecord *sync.Map // 任务
}
type Job func(TaskData)
type TaskData map[interface{}]interface{}
type task struct {
interval time.Duration
times int
circle int
key interface{}
job Job
taskData TaskData
}
// NewTimeWheel 时间间隔
// 轮盘大小
func NewTimeWheel(interval time.Duration, slotNam int) *TimeWheel {
if interval <= 0 || slotNam <= 0 {
return nil
}
tw := &TimeWheel{
interval: interval,
slots: make([]*list.List, slotNam),
currentPos: 0,
slotNum: slotNam,
addTaskChan: make(chan *task),
stopChan: make(chan bool),
taskRecord: &sync.Map{},
}
tw.init()
return tw
}
func (tw *TimeWheel) Start() {
tw.ticker = time.NewTicker(tw.interval)
go tw.start()
}
func (tw *TimeWheel) Stop() {
tw.stopChan <- true
}
func (tw *TimeWheel) start() {
defer func() {
fmt.Println("warn: TimeWheel stop")
}()
for {
select {
case <-tw.ticker.C:
tw.tickHandle()
case task := <-tw.addTaskChan:
tw.addTask(task)
case <-tw.stopChan:
tw.ticker.Stop()
return
}
}
}
func (tw *TimeWheel) RemoveTask(key interface{}) error {
if key == nil {
return nil
}
value, ok := tw.taskRecord.Load(key)
if !ok {
return errors.New("task not exists")
} else {
task := value.(*task)
task.times = 0
tw.taskRecord.Delete(task.key)
}
return nil
}
func (tw *TimeWheel) UpdateTask(key interface{}, interval time.Duration, data TaskData) error {
if key == nil {
return errors.New("illegal key, please try again")
}
value, ok := tw.taskRecord.Load(key)
if !ok {
return errors.New("task not exists, please check you task key")
}
task := value.(*task)
task.taskData = data
task.interval = interval
return nil
}
func (tw *TimeWheel) init() {
for i := 0; i < tw.slotNum; i++ {
tw.slots[i] = list.New()
}
}
// AddTask 添加任务 到时间轮
// interval 时间间隔
// 次数
// 索引
// 参数
// 任务
func (tw *TimeWheel) AddTask(interval time.Duration, times int, key interface{}, data TaskData, job Job) error {
if interval <= 0 || key == nil || job == nil || times < -1 || times == 0 {
return errors.New("illegal task params")
}
if _, ok := tw.taskRecord.Load(key); ok {
return errors.New("duplicate task key")
}
tw.addTaskChan <- &task{interval: interval, times: times, key: key, taskData: data, job: job}
return nil
}
func (tw *TimeWheel) tickHandle() {
l := tw.slots[tw.currentPos]
tw.scanAddRunTask(l)
if tw.currentPos == tw.slotNum-1 {
tw.currentPos = 0
} else {
tw.currentPos++
}
}
func (tw *TimeWheel) addTask(task *task) {
if task.times == 0 {
return
}
pos, circle := tw.getPositionAndCircle(task.interval)
task.circle = circle
tw.slots[pos].PushBack(task)
tw.taskRecord.Store(task.key, task)
}
func (tw *TimeWheel) scanAddRunTask(l *list.List) {
if l == nil {
return
}
for item := l.Front(); item != nil; {
task := item.Value.(*task)
if task.times == 0 { // 次数为0 删除任务
next := item.Next()
l.Remove(item)
tw.taskRecord.Delete(task.key)
item = next
continue
}
if task.circle > 0 { // 计数器
task.circle--
item = item.Next()
continue
}
go task.job(task.taskData) // 执行任务
next := item.Next()
l.Remove(item)
item = next
if task.times == 1 {
task.times = 0
tw.taskRecord.Delete(task.key)
} else {
if task.times > 0 {
task.times--
}
tw.addTask(task)
}
}
}
func (tw *TimeWheel) getPositionAndCircle(d time.Duration) (pos int, circle int) {
delaySeconds := int(d.Seconds())
intervalSeconds := int(tw.interval.Seconds())
// 圈数
circle = int(delaySeconds / intervalSeconds / tw.slotNum)
// 指针
pos = int(tw.currentPos+delaySeconds/intervalSeconds) % tw.slotNum
return
}