-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimewheel.go
298 lines (242 loc) · 5.23 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package helper
import (
"errors"
"sync"
"sync/atomic"
"time"
)
const (
modeIsCircle = true
modeNotCircle = false
modeIsAsync = true
modeNotAsync = false
)
type taskID int64
type Task struct {
delay time.Duration
id taskID
round int
callback func()
async bool
stop bool
circle bool
}
type TimeWheel struct {
randomID int64
tick time.Duration
ticker *time.Ticker
tickQueue chan time.Time
bucketsNum int
buckets []map[taskID]*Task
bucketIndexes map[taskID]int
currentIndex int
onceStart sync.Once
addC chan *Task
removeC chan *Task
stopC chan struct{}
exited bool
}
// NewTimeWheel 创建一个时间轮
func NewTimeWheel(tick time.Duration, bucketsNum int, safeMode bool) (*TimeWheel, error) {
if tick.Seconds() < 0.1 {
return nil, errors.New("invalid params, must tick >= 100 ms")
}
if bucketsNum <= 0 {
return nil, errors.New("invalid params, must bucketsNum > 0")
}
tw := &TimeWheel{
// tick
tick: tick,
// store
bucketsNum: bucketsNum,
bucketIndexes: make(map[taskID]int, 1024*100),
buckets: make([]map[taskID]*Task, bucketsNum),
currentIndex: 0,
// signal
addC: make(chan *Task, 1024*5),
removeC: make(chan *Task, 1024*2),
stopC: make(chan struct{}),
}
for i := 0; i < bucketsNum; i++ {
tw.buckets[i] = make(map[taskID]*Task, 16)
}
if safeMode {
tw.tickQueue = make(chan time.Time, 10)
}
return tw, nil
}
// Start 启动时间轮
func (tw *TimeWheel) Start() {
// 只允许启动一次
tw.onceStart.Do(
func() {
tw.ticker = time.NewTicker(tw.tick)
go tw.schduler()
go tw.tickGenerator()
},
)
}
// Add 添加任务
func (tw *TimeWheel) Add(delay time.Duration, callback func()) *Task {
return tw.addAny(delay, callback, modeNotCircle, modeIsAsync)
}
// AddCron 添加循环定时任务
func (tw *TimeWheel) AddCron(delay time.Duration, callback func()) *Task {
return tw.addAny(delay, callback, modeIsCircle, modeIsAsync)
}
//删除任务
func (tw *TimeWheel) Remove(task *Task) error {
tw.removeC <- task
return nil
}
// Sleep 睡眠
func (tw *TimeWheel) Sleep(delay time.Duration) {
queue := make(chan bool, 1)
tw.addAny(delay,
func() {
queue <- true
},
modeNotCircle, modeNotAsync,
)
<-queue
}
func (tw *TimeWheel) After(delay time.Duration) <-chan time.Time {
queue := make(chan time.Time, 1)
tw.addAny(delay,
func() {
queue <- time.Now()
},
modeNotCircle, modeNotAsync,
)
return queue
}
// Stop 停止时间轮
func (tw *TimeWheel) Stop() {
tw.stopC <- struct{}{}
}
//生成时间间隔
func (tw *TimeWheel) tickGenerator() {
if tw.tickQueue == nil {
return
}
for !tw.exited {
select {
case <-tw.ticker.C:
select {
case tw.tickQueue <- time.Now():
default:
panic("raise long time blocking")
}
}
}
}
//定时执行任务
func (tw *TimeWheel) schduler() {
queue := tw.ticker.C
if tw.tickQueue != nil {
queue = tw.tickQueue
}
for {
select {
case <-queue:
tw.handleTick()
case task := <-tw.addC:
tw.put(task)
case key := <-tw.removeC:
tw.remove(key)
case <-tw.stopC: //停止
tw.exited = true
tw.ticker.Stop()
return
}
}
}
//清理任务
func (tw *TimeWheel) cleanTask(task *Task) {
index := tw.bucketIndexes[task.id]
delete(tw.bucketIndexes, task.id)
delete(tw.buckets[index], task.id)
}
func (tw *TimeWheel) handleTick() {
bucket := tw.buckets[tw.currentIndex]
for k, task := range bucket {
if task.stop {
tw.cleanTask(task)
continue
}
if bucket[k].round > 0 {
bucket[k].round--
continue
}
if task.async {
go task.callback()
} else {
// optimize gopool
task.callback()
}
// 循环执行
if task.circle {
tw.cleanTask(task)
tw.putCircle(task, modeIsCircle)
continue
}
// gc
tw.cleanTask(task)
}
if tw.currentIndex == tw.bucketsNum-1 {
tw.currentIndex = 0
return
}
tw.currentIndex++
}
func (tw *TimeWheel) addAny(delay time.Duration, callback func(), circle, async bool) *Task {
if delay <= 0 {
delay = tw.tick
}
id := tw.genUniqueID()
var task = new(Task)
task.delay = delay
task.id = id
task.callback = callback
task.circle = circle
task.async = async
tw.addC <- task
return task
}
func (tw *TimeWheel) put(task *Task) {
tw.store(task, false)
}
func (tw *TimeWheel) putCircle(task *Task, circleMode bool) {
tw.store(task, circleMode)
}
func (tw *TimeWheel) store(task *Task, circleMode bool) {
round := tw.calculateRound(task.delay)
index := tw.calculateIndex(task.delay)
if round > 0 && circleMode {
task.round = round - 1
} else {
task.round = round
}
tw.bucketIndexes[task.id] = index
tw.buckets[index][task.id] = task
}
func (tw *TimeWheel) calculateRound(delay time.Duration) (round int) {
delaySeconds := delay.Seconds()
tickSeconds := tw.tick.Seconds()
round = int(delaySeconds / tickSeconds / float64(tw.bucketsNum))
return
}
func (tw *TimeWheel) calculateIndex(delay time.Duration) (index int) {
delaySeconds := delay.Seconds()
tickSeconds := tw.tick.Seconds()
index = (int(float64(tw.currentIndex) + delaySeconds/tickSeconds)) % tw.bucketsNum
return
}
func (tw *TimeWheel) remove(task *Task) {
tw.cleanTask(task)
}
//生成唯一任务ID
func (tw *TimeWheel) genUniqueID() taskID {
id := atomic.AddInt64(&tw.randomID, 1)
return taskID(id)
}