-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimingwheel.go
444 lines (405 loc) · 9.55 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
* Copyright (c) 2021 Austin Zhai <singchia@163.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*/
package timer
import (
"errors"
"sync"
"sync/atomic"
"time"
"github.com/singchia/go-timer/v2/pkg/scheduler"
)
const (
defaultTickInterval time.Duration = 10 * time.Millisecond
defaultTicks uint64 = 1024 * 1024 * 1024 * 1024
defaultSlots uint = 256
)
var (
ErrDurationOutOfRange = errors.New("duration out of range")
ErrTimerNotStarted = errors.New("timer not started")
ErrTimerForceClosed = errors.New("timer force closed")
ErrOperationForceClosed = errors.New("operation force closed")
ErrDelayOnCyclically = errors.New("cannot delay on cyclically tick")
ErrCancelOnNonWait = errors.New("cannot cancel on firing or fired tick")
)
const (
twStatusInit = iota
twStatusStarted
twStatusPaused
twStatusStoped
)
// operation type and return
type operType int
const (
operAdd operType = iota
operCancel
operDelay
operReset
)
type operation struct {
tick *tick
operType operType
retCh chan *operationRet
// for delay
delay time.Duration
// for reset
data interface{}
}
type operRetType int
const (
operOK operRetType = iota
operErr
)
type operationRet struct {
operRetType operRetType
err error
}
// timer options
type timerOption struct {
interval time.Duration
operationBufferSize int
}
type timingwheel struct {
*timerOption
mtx sync.RWMutex
twStatus int
wheels []*wheel
max uint64
sch *scheduler.Scheduler
operations chan *operation
pause, quit chan struct{}
}
func newTimingwheel(opts ...TimerOption) *timingwheel {
max, length := calcuWheels(defaultTicks)
tw := &timingwheel{
twStatus: twStatusInit,
sch: scheduler.NewScheduler(),
timerOption: &timerOption{
interval: defaultTickInterval,
operationBufferSize: 128,
},
pause: make(chan struct{}),
quit: make(chan struct{}),
}
for _, opt := range opts {
opt(tw.timerOption)
}
tw.operations = make(chan *operation, tw.operationBufferSize)
tw.setWheels(max, length)
tw.sch.SetMaxGoroutines(1000)
tw.sch.StartSchedule()
tw.Start()
return tw
}
func (tw *timingwheel) setWheels(max uint64, length int) {
tw.wheels = make([]*wheel, 0, length)
tw.max = max
for i := uint(0); i < uint(length); i++ {
tw.wheels = append(tw.wheels, newWheel(defaultSlots, i))
}
}
func (tw *timingwheel) Add(d time.Duration, opts ...TickOption) Tick {
tick := &tick{
duration: d,
tw: tw,
insertTime: time.Now(),
tickOption: &tickOption{},
status: statusAdd,
}
for _, opt := range opts {
opt(tick.tickOption)
}
if tick.ch == nil && tick.handler == nil {
tick.ch = make(chan *Event, 1)
}
tw.mtx.RLock()
defer tw.mtx.RUnlock()
if tw.twStatus != twStatusStarted {
tw.handleError(tick, ErrTimerNotStarted)
return tick
}
tw.operations <- &operation{
tick: tick,
operType: operAdd,
}
return tick
}
func (tw *timingwheel) Start() {
tw.mtx.Lock()
defer tw.mtx.Unlock()
if tw.twStatus != twStatusInit && tw.twStatus != twStatusPaused {
return
}
tw.twStatus = twStatusStarted
go tw.drive()
return
}
func (tw *timingwheel) Pause() {
tw.mtx.Lock()
defer tw.mtx.Unlock()
if tw.twStatus != twStatusStarted {
return
}
tw.twStatus = twStatusPaused
tw.pause <- struct{}{}
return
}
func (tw *timingwheel) Moveon() {
tw.mtx.Lock()
defer tw.mtx.Unlock()
if tw.twStatus != twStatusPaused {
return
}
tw.twStatus = twStatusStarted
go tw.drive()
}
func (tw *timingwheel) Close() {
tw.mtx.Lock()
defer tw.mtx.Unlock()
if tw.twStatus != twStatusStarted {
return
}
close(tw.quit)
tw.twStatus = twStatusStoped
}
func (tw *timingwheel) drive() {
driver := time.NewTicker(tw.interval)
defer driver.Stop()
for {
select {
case <-driver.C:
// fire all ready tick
for _, wheel := range tw.wheels {
linker := wheel.incN(1)
if linker != nil && linker.Length() > 0 {
linker.Foreach(tw.iterate)
}
if wheel.cur != 0 {
break
}
}
case operation, ok := <-tw.operations:
if !ok {
continue
}
switch operation.operType {
case operAdd:
// the specific tick's add operation must be before the del or delay operation
// so we don't care the status cause it must be statusAdd
ipw := tw.indexesPerWheel(operation.tick.duration)
operation.tick.ipw = ipw
tw.wheels[len(ipw)-1].add(ipw[len(ipw)-1], operation.tick)
operation.tick.status = statusWait
case operCancel:
// only in wait status can delete the tick
if operation.tick.status != statusWait {
operation.retCh <- &operationRet{
operRetType: operErr,
err: ErrCancelOnNonWait,
}
continue
}
operation.tick.s.delete(operation.tick)
operation.retCh <- &operationRet{
operRetType: operOK,
}
operation.tick.status = statusCanceled
case operDelay:
if operation.tick.status != statusWait {
operation.retCh <- &operationRet{
operRetType: operErr,
err: ErrCancelOnNonWait,
}
continue
}
operation.tick.s.delete(operation.tick)
ipw := tw.indexesPerWheelBased(operation.delay, operation.tick.ipw)
operation.tick.ipw = ipw
operation.tick.delay = operation.delay
tw.wheels[len(ipw)-1].add(ipw[len(ipw)-1], operation.tick)
operation.retCh <- &operationRet{
operRetType: operOK,
}
case operReset:
if operation.tick.status != statusWait {
operation.retCh <- &operationRet{
operRetType: operErr,
err: ErrCancelOnNonWait,
}
continue
}
operation.tick.data = operation.data
operation.retCh <- &operationRet{
operRetType: operOK,
}
}
case <-tw.pause:
return
case <-tw.quit:
close(tw.operations)
for operation := range tw.operations {
switch operation.operType {
case operCancel, operDelay, operReset:
operation.retCh <- &operationRet{
operRetType: operErr,
err: ErrTimerForceClosed,
}
case operAdd:
tw.handleError(operation.tick, ErrTimerForceClosed)
continue
}
}
for _, wheel := range tw.wheels {
for _, slot := range wheel.slots {
slot.foreach(tw.forceClose)
}
}
// accelerate gc
tw.sch.Close()
tw.wheels = nil
tw.operations = nil
tw.sch = nil
tw.quit = nil
tw.pause = nil
return
}
}
}
func (tw *timingwheel) iterate(data interface{}) error {
tk, _ := data.(*tick)
if tk.status == statusCanceled {
return nil
}
position := tk.s.w.position
for position > 0 {
position--
if tk.ipw[position] > 0 {
tw.wheels[position].add(tk.ipw[position], tk)
return nil
}
}
tk.status = statusFire
if !tk.cyclically {
tw.sch.PublishRequest(&scheduler.Request{Data: tk, Handler: tw.handleNormal})
return nil
}
// copy the tick, since the data might be Reset.
tickCopy := &tick{
tickOption: &tickOption{
data: tk.data,
ch: tk.ch,
handler: tk.handler,
},
insertTime: tk.insertTime,
duration: tk.duration,
fired: tk.fired,
}
tw.sch.PublishRequest(&scheduler.Request{Data: tickCopy, Handler: tw.handleNormal})
ipw := tw.indexesPerWheelBased(tk.duration, tk.ipw)
tk.ipw = ipw
tw.wheels[len(ipw)-1].add(ipw[len(ipw)-1], tk)
tk.status = statusWait
return nil
}
func (tw *timingwheel) forceClose(data interface{}) error {
tick, _ := data.(*tick)
if tick.status == statusCanceled {
return nil
}
tick.status = statusFire
tw.sch.PublishRequest(&scheduler.Request{Data: tick, Handler: tw.handleForceClosed})
return nil
}
func (tw *timingwheel) handleForceClosed(data interface{}) {
tw.handleError(data, ErrTimerForceClosed)
}
func (tw *timingwheel) handleError(data interface{}, err error) {
tk, _ := data.(*tick)
if tk.status == statusCanceled {
return
}
event := &Event{
Duration: tk.duration,
InsertTIme: tk.insertTime,
Data: tk.data,
Error: err,
}
if tk.ch == nil {
tk.handler(event)
} else {
tk.ch <- event
if !tk.chOutside {
close(tk.ch)
}
}
}
func (tw *timingwheel) handleNormal(data interface{}) {
tk, _ := data.(*tick)
if tk.status == statusCanceled {
return
}
atomic.AddInt64(&tk.fired, 1)
event := &Event{
Duration: tk.duration,
InsertTIme: tk.insertTime,
Data: tk.data,
Error: nil,
}
if tk.ch == nil {
tk.handler(event)
} else {
tk.ch <- event
}
}
func (tw *timingwheel) indexesPerWheel(d time.Duration) []uint {
var ipw []uint
var reminder uint64
var quotient = uint64((d + tw.interval - 1) / tw.interval)
for i, wheel := range tw.wheels {
if quotient == 0 {
break
}
quotient += uint64(tw.wheels[i].cur)
reminder = quotient % uint64(wheel.numSlots)
quotient = quotient / uint64(wheel.numSlots)
ipw = append(ipw, uint(reminder))
}
return ipw
}
func (tw *timingwheel) indexesPerWheelBased(d time.Duration, base []uint) []uint {
var ipw []uint
var reminder uint64
var quotient = uint64((d + tw.interval - 1) / tw.interval)
for i, wheel := range tw.wheels {
if quotient == 0 {
break
}
if len(base) <= i {
quotient += uint64(tw.wheels[i].cur)
} else {
quotient += uint64(base[i])
}
reminder = quotient % uint64(wheel.numSlots)
quotient = quotient / uint64(wheel.numSlots)
ipw = append(ipw, uint(reminder))
}
return ipw
}
func calcuWheels(num uint64) (uint64, int) {
count := uint64(defaultSlots)
length := 1
for {
if count < num {
count *= uint64(defaultSlots)
length += 1
continue
}
break
}
return count, length
}