-
Notifications
You must be signed in to change notification settings - Fork 17
/
timerwheel.go
139 lines (123 loc) · 3.19 KB
/
timerwheel.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
package internal
import (
"math/bits"
"time"
"github.com/Yiling-J/theine-go/internal/clock"
)
func next2Power(x uint) uint {
x--
x |= x >> 1
x |= x >> 2
x |= x >> 4
x |= x >> 8
x |= x >> 16
x |= x >> 32
x++
return x
}
type TimerWheel[K comparable, V any] struct {
clock *clock.Clock
buckets []uint
spans []uint
shift []uint
wheel [][]*List[K, V]
nanos int64
}
func NewTimerWheel[K comparable, V any](size uint) *TimerWheel[K, V] {
clock := &clock.Clock{Start: time.Now().UTC()}
buckets := []uint{64, 64, 32, 4, 1}
spans := []uint{
next2Power(uint((1 * time.Second).Nanoseconds())),
next2Power(uint((1 * time.Minute).Nanoseconds())),
next2Power(uint((1 * time.Hour).Nanoseconds())),
next2Power(uint((24 * time.Hour).Nanoseconds())),
next2Power(uint((24 * time.Hour).Nanoseconds())) * 4,
next2Power(uint((24 * time.Hour).Nanoseconds())) * 4,
}
shift := []uint{
uint(bits.TrailingZeros(spans[0])),
uint(bits.TrailingZeros(spans[1])),
uint(bits.TrailingZeros(spans[2])),
uint(bits.TrailingZeros(spans[3])),
uint(bits.TrailingZeros(spans[4])),
}
wheel := [][]*List[K, V]{}
for i := 0; i < 5; i++ {
tmp := []*List[K, V]{}
for j := 0; j < int(buckets[i]); j++ {
tmp = append(tmp, NewList[K, V](0, WHEEL_LIST))
}
wheel = append(wheel, tmp)
}
return &TimerWheel[K, V]{
buckets: buckets,
spans: spans,
shift: shift,
wheel: wheel,
nanos: clock.NowNano(),
clock: clock,
}
}
func (tw *TimerWheel[K, V]) findIndex(expire int64) (int, int) {
duration := expire - tw.nanos
for i := 0; i < 5; i++ {
if duration < int64(tw.spans[i+1]) {
ticks := expire >> int(tw.shift[i])
slot := int(ticks) & (int(tw.buckets[i]) - 1)
return i, slot
}
}
return 4, 0
}
func (tw *TimerWheel[K, V]) deschedule(entry *Entry[K, V]) {
entry.prev(WHEEL_LIST).setNext(entry.next(WHEEL_LIST), WHEEL_LIST)
entry.next(WHEEL_LIST).setPrev(entry.prev(WHEEL_LIST), WHEEL_LIST)
entry.setNext(nil, WHEEL_LIST)
entry.setPrev(nil, WHEEL_LIST)
}
func (tw *TimerWheel[K, V]) schedule(entry *Entry[K, V]) {
if entry.meta.wheelPrev != nil {
tw.deschedule(entry)
}
x, y := tw.findIndex(entry.expire.Load())
tw.wheel[x][y].PushFront(entry)
}
func (tw *TimerWheel[K, V]) advance(now int64, remove func(entry *Entry[K, V], reason RemoveReason)) {
if now == 0 {
now = tw.clock.NowNano()
}
previous := tw.nanos
tw.nanos = now
for i := 0; i < 5; i++ {
prevTicks := previous >> int64(tw.shift[i])
currentTicks := tw.nanos >> int64(tw.shift[i])
if currentTicks <= prevTicks {
break
}
tw.expire(i, prevTicks, currentTicks-prevTicks, remove)
}
}
func (tw *TimerWheel[K, V]) expire(index int, prevTicks int64, delta int64, remove func(entry *Entry[K, V], reason RemoveReason)) {
mask := tw.buckets[index] - 1
steps := tw.buckets[index]
if delta < int64(steps) {
steps = uint(delta)
}
start := prevTicks & int64(mask)
end := start + int64(steps)
for i := start; i < end; i++ {
list := tw.wheel[index][i&int64(mask)]
entry := list.Front()
for entry != nil {
next := entry.Next(WHEEL_LIST)
if entry.expire.Load() <= tw.nanos {
tw.deschedule(entry)
remove(entry, EXPIRED)
} else {
tw.schedule(entry)
}
entry = next
}
list.Reset()
}
}