/
timewheel.go
402 lines (340 loc) · 9.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
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
package goetty
import (
"errors"
"runtime"
"sync"
"sync/atomic"
"time"
"unsafe"
)
// Timeout Hash Wheel / Calendar Queue implementation of a callout wheel for timeouts.
// Ticking happens from a time.Ticker
const (
defaultTickInterval = time.Millisecond
defaultNumBuckets = 2048
cacheline = 64
bitsInUint64 = 64
)
const (
// states of the TimeoutWheel
stopped = iota
stopping
running
)
const (
// states of a Timeout
timeoutInactive = iota
timeoutExpired
timeoutActive
)
var (
// ErrSystemStopped is returned when a user tries to schedule a timeout after stopping the
// timeout system.
ErrSystemStopped = errors.New("Timeout System is stopped")
// 4*(NumCPU rounded down to the next power of 2.)
defaultPoolSize = uint64(1 << uint(findMSB(uint64(runtime.NumCPU()))+2))
)
// Timeout represents a single timeout function pending expiration.
type Timeout struct {
generation uint64
timeout *timeout
}
// Stop stops the scheduled timeout so that the callback will not be called. It returns true if it
// successfully canceled
func (t *Timeout) Stop() bool {
if t.timeout == nil {
return false
}
t.timeout.mtx.Lock()
if t.timeout.generation != t.generation || t.timeout.state != timeoutActive {
t.timeout.mtx.Unlock()
return false
}
t.timeout.removeLocked()
t.timeout.wheel.putTimeoutLocked(t.timeout)
t.timeout.mtx.Unlock()
return true
}
type timeout struct {
mtx *paddedMutex
expireCb func(interface{})
expireArg interface{}
deadline uint64
// list pointers for the freelist/buckets of the queue. The list is implemented as a forward
// pointer and a pointer to the address of the previous next field. It is doubly linked in this
// manner so that removal does not require traversal. It can only be iterated in the forward.
next *timeout
prev **timeout
state int32
wheel *TimeoutWheel
generation uint64
}
type timeoutList struct {
lastTick uint64
head *timeout
}
func (t *timeout) prependLocked(list *timeoutList) {
if list.head != nil {
t.prev = list.head.prev
list.head.prev = &t.next
} else {
t.prev = &list.head
}
t.next = list.head
list.head = t
}
func (t *timeout) removeLocked() {
if t.next != nil {
t.next.prev = t.prev
}
*t.prev = t.next
t.next = nil
t.prev = nil
}
// TimeoutWheel is a bucketed collection of Timeouts that have a deadline in the future.
// (current tick granularity is 1ms).
type TimeoutWheel struct {
// ticks is an atomic
ticks uint64
// buckets[i] and freelists[i] is locked by mtxPool[i&poolMask]
mtxPool []paddedMutex
bucketMask uint64
poolMask uint64
tickInterval time.Duration
buckets []timeoutList
freelists []timeoutList
state int
calloutCh chan timeoutList
done chan struct{}
}
// Option is a configuration option to NewTimeoutWheel
type Option func(*opts)
type opts struct {
tickInterval time.Duration
size uint64
poolsize uint64
}
// sync.Mutex padded to a cache line to keep the locks from false sharing with each other
type paddedMutex struct {
sync.Mutex
_ [cacheline - unsafe.Sizeof(sync.Mutex{})]byte
}
// WithTickInterval sets the frequency of ticks.
func WithTickInterval(interval time.Duration) Option {
return func(opts *opts) { opts.tickInterval = interval }
}
// WithBucketsExponent sets the number of buckets in the hash table.
func WithBucketsExponent(bucketExp uint) Option {
return func(opts *opts) {
opts.size = uint64(1 << bucketExp)
}
}
// WithLocksExponent sets the number locks in the lockpool used to lock the time buckets. If the
// number is greater than the number of buckets, the number of buckets will be used instead.
func WithLocksExponent(lockExp uint) Option {
return func(opts *opts) {
opts.poolsize = uint64(1 << lockExp)
}
}
// NewTimeoutWheel creates and starts a new TimeoutWheel collection.
func NewTimeoutWheel(options ...Option) *TimeoutWheel {
opts := &opts{
tickInterval: defaultTickInterval,
size: defaultNumBuckets,
poolsize: defaultPoolSize,
}
for _, option := range options {
option(opts)
}
poolsize := opts.poolsize
if opts.size < opts.poolsize {
poolsize = opts.size
}
t := &TimeoutWheel{
mtxPool: make([]paddedMutex, poolsize),
freelists: make([]timeoutList, poolsize),
state: stopped,
poolMask: poolsize - 1,
buckets: make([]timeoutList, opts.size),
tickInterval: opts.tickInterval,
bucketMask: opts.size - 1,
ticks: 0,
}
t.Start()
return t
}
// Start starts a stopped timeout wheel. Subsequent calls to Start panic.
func (t *TimeoutWheel) Start() {
t.lockAllBuckets()
defer t.unlockAllBuckets()
for t.state != stopped {
switch t.state {
case stopping:
t.unlockAllBuckets()
<-t.done
t.lockAllBuckets()
case running:
panic("Tried to start a running TimeoutWheel")
}
}
t.state = running
t.done = make(chan struct{})
t.calloutCh = make(chan timeoutList)
go t.doTick()
go t.doExpired()
}
// Stop stops tick processing, and deletes any remaining timeouts.
func (t *TimeoutWheel) Stop() {
t.lockAllBuckets()
if t.state == running {
t.state = stopping
close(t.calloutCh)
for i := range t.buckets {
t.freeBucketLocked(t.buckets[i])
}
}
// unlock so the callouts can finish.
t.unlockAllBuckets()
<-t.done
}
// Schedule adds a new function to be called after some duration of time has
// elapsed. The returned Timeout can be used to cancel calling the function. If the duration falls
// between two ticks, the latter tick is used.
func (t *TimeoutWheel) Schedule(
d time.Duration,
expireCb func(interface{}),
arg interface{},
) (Timeout, error) {
dTicks := (d + t.tickInterval - 1) / t.tickInterval
deadline := atomic.LoadUint64(&t.ticks) + uint64(dTicks)
timeout := t.getTimeoutLocked(deadline)
if t.state != running {
t.putTimeoutLocked(timeout)
timeout.mtx.Unlock()
return Timeout{}, ErrSystemStopped
}
bucket := &t.buckets[deadline&t.bucketMask]
timeout.expireCb = expireCb
timeout.expireArg = arg
timeout.deadline = deadline
timeout.state = timeoutActive
out := Timeout{timeout: timeout, generation: timeout.generation}
// execute the callback now, return a Timeout that is already Stopped (generation is bumped)
if bucket.lastTick >= deadline {
t.putTimeoutLocked(timeout)
timeout.mtx.Unlock()
expireCb(arg)
return out, nil
}
timeout.prependLocked(bucket)
timeout.mtx.Unlock()
return out, nil
}
// doTick handles the ticker goroutine of the timer system
func (t *TimeoutWheel) doTick() {
var expiredList timeoutList
ticker := time.NewTicker(t.tickInterval)
for range ticker.C {
atomic.AddUint64(&t.ticks, 1)
mtx := t.lockBucket(t.ticks)
if t.state != running {
mtx.Unlock()
break
}
bucket := &t.buckets[t.ticks&t.bucketMask]
timeout := bucket.head
bucket.lastTick = t.ticks
// find all the expired timeouts in the bucket.
for timeout != nil {
next := timeout.next
if timeout.deadline <= t.ticks {
timeout.state = timeoutExpired
timeout.removeLocked()
timeout.prependLocked(&expiredList)
}
timeout = next
}
mtx.Unlock()
if expiredList.head == nil {
continue
}
select {
case t.calloutCh <- expiredList:
expiredList.head = nil
default:
}
}
ticker.Stop()
}
func (t *TimeoutWheel) getTimeoutLocked(deadline uint64) *timeout {
mtx := &t.mtxPool[deadline&t.poolMask]
mtx.Lock()
freelist := &t.freelists[deadline&t.poolMask]
if freelist.head == nil {
timeout := &timeout{mtx: mtx, wheel: t}
return timeout
}
timeout := freelist.head
timeout.removeLocked()
return timeout
}
func (t *TimeoutWheel) putTimeoutLocked(timeout *timeout) {
freelist := &t.freelists[timeout.deadline&t.poolMask]
timeout.state = timeoutInactive
timeout.generation++
timeout.prependLocked(freelist)
}
func (t *TimeoutWheel) lockBucket(bucket uint64) *paddedMutex {
mtx := &t.mtxPool[bucket&t.poolMask]
mtx.Lock()
return mtx
}
func (t *TimeoutWheel) lockAllBuckets() {
for i := range t.mtxPool {
t.mtxPool[i].Lock()
}
}
func (t *TimeoutWheel) unlockAllBuckets() {
for i := len(t.mtxPool) - 1; i >= 0; i-- {
t.mtxPool[i].Unlock()
}
}
func (t *TimeoutWheel) freeBucketLocked(head timeoutList) {
timeout := head.head
for timeout != nil {
next := timeout.next
timeout.removeLocked()
t.putTimeoutLocked(timeout)
timeout = next
}
}
func (t *TimeoutWheel) doExpired() {
for list := range t.calloutCh {
timeout := list.head
for timeout != nil {
timeout.mtx.Lock()
next := timeout.next
expireCb := timeout.expireCb
expireArg := timeout.expireArg
t.putTimeoutLocked(timeout)
timeout.mtx.Unlock()
if expireCb != nil {
expireCb(expireArg)
}
timeout = next
}
}
t.lockAllBuckets()
t.state = stopped
t.unlockAllBuckets()
close(t.done)
}
// return the bit position of the most significant bit of the number passed in (base zero)
func findMSB(value uint64) int {
for i := bitsInUint64 - 1; i >= 0; i-- {
if value&(1<<uint(i)) != 0 {
return int(i)
}
}
return -1
}