-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheel.go
61 lines (49 loc) · 1.04 KB
/
wheel.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
package timer
import (
"sync"
"time"
)
type Wheel struct {
ticker *time.Ticker
wheels []chan struct{}
gearSize time.Duration
wheelNum int
current int
lock sync.RWMutex
}
// NewWheel new timewheel with gearSize and wheelNum.
func NewWheel(wheelNum int, gearSize time.Duration) *Wheel {
w := &Wheel{
gearSize: gearSize,
wheelNum: wheelNum + 1,
wheels: make([]chan struct{}, wheelNum+1),
}
for i := 0; i < wheelNum+1; i++ {
w.wheels[i] = make(chan struct{})
}
w.ticker = time.NewTicker(gearSize)
go w.run()
return w
}
// Stop the timewheel
func (w *Wheel) Stop() {
w.ticker.Stop()
}
// After like time.After
func (w *Wheel) After(timeout time.Duration) <-chan struct{} {
w.lock.RLock()
index := (int(timeout/w.gearSize) + w.current) % w.wheelNum
c := w.wheels[index]
w.lock.RUnlock()
return c
}
func (w *Wheel) run() {
for range w.ticker.C {
w.lock.Lock()
oldestC := w.wheels[w.current]
w.wheels[w.current] = make(chan struct{})
w.current = (w.current + 1) % w.wheelNum
w.lock.Unlock()
close(oldestC)
}
}