-
Notifications
You must be signed in to change notification settings - Fork 0
/
hourglass.go
250 lines (227 loc) · 5.87 KB
/
hourglass.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
// Copyright 2023 Ailn(ailnindex@qq.com). All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package hourglass provide a timing wheel for go
package hourglass
import (
"context"
"sync"
"time"
)
var hourglass = NewHourglass(time.Second, 60)
// Hourglass is a timing wheel
type Hourglass struct {
tickDuration time.Duration
cancel context.CancelFunc
rootWheel *wheel
}
// NewHourglass make an hourglass with tickDuration and ticksPerWheel
func NewHourglass(tickDuration time.Duration, ticksPerWheel int) *Hourglass {
ctx, cancel := context.WithCancel(context.TODO())
hourglass := Hourglass{
tickDuration: tickDuration,
cancel: cancel,
rootWheel: &wheel{
currentTick: 0,
tickDuration: tickDuration,
ticksPerWheel: ticksPerWheel,
tickers: make([]map[*Ticker]struct{}, ticksPerWheel),
locker: sync.Mutex{},
},
}
go hourglass.run(ctx)
return &hourglass
}
// Close hourglass
func (h *Hourglass) Close() {
h.cancel()
}
func (h *Hourglass) run(ctx context.Context) {
ticker := time.NewTicker(h.tickDuration)
defer ticker.Stop()
loop:
for {
select {
case <-ctx.Done():
break loop
case t := <-ticker.C:
h.rootWheel.tick(t)
}
}
}
// Ticker provide a time ticker like time.Ticker
type Ticker struct {
duration time.Duration
arg any
f func(arg any, t time.Time)
closed bool
locker sync.RWMutex
}
// C is channel of time.Time
func (t *Ticker) C() <-chan time.Time {
return t.arg.(chan time.Time)
}
// Close ticker
func (t *Ticker) Close() {
t.locker.Lock()
defer t.locker.Unlock()
t.closed = true
}
// NewTicker make a ticker every duration from hourglass
func (h *Hourglass) NewTicker(duration time.Duration) *Ticker {
duration = formatDuration(h.tickDuration, duration)
ticker := Ticker{
duration: duration,
arg: make(chan time.Time, 1),
f: sendTime,
locker: sync.RWMutex{},
}
h.rootWheel.setTicker(&ticker)
return &ticker
}
// After duration will send a tick to Ticker
func (h *Hourglass) After(duration time.Duration) *Ticker {
duration = formatDuration(h.tickDuration, duration)
ticker := Ticker{
duration: duration,
arg: make(chan time.Time, 1),
f: sendTime,
locker: sync.RWMutex{},
}
defer ticker.Close()
h.rootWheel.setTicker(&ticker)
return &ticker
}
// AfterFunc after duration func will be execution
func (h *Hourglass) AfterFunc(duration time.Duration, f func(t time.Time)) {
duration = formatDuration(h.tickDuration, duration)
ticker := Ticker{
duration: duration,
arg: f,
f: goFunc,
locker: sync.RWMutex{},
}
defer ticker.Close()
h.rootWheel.setTicker(&ticker)
}
// After duration default hourglass will send a tick to Ticker
func After(duration time.Duration) *Ticker {
duration = formatDuration(hourglass.tickDuration, duration)
ticker := Ticker{
duration: duration,
arg: make(chan time.Time, 1),
f: sendTime,
locker: sync.RWMutex{},
}
defer ticker.Close()
hourglass.rootWheel.setTicker(&ticker)
return &ticker
}
// AfterFunc default hourglass after duration func will be execution
func AfterFunc(duration time.Duration, f func(t time.Time)) {
duration = formatDuration(hourglass.tickDuration, duration)
ticker := Ticker{
duration: duration,
arg: f,
f: goFunc,
locker: sync.RWMutex{},
}
defer ticker.Close()
hourglass.rootWheel.setTicker(&ticker)
}
// NewTicker make a ticker use default hourglass with 1 second tickDuration and 60 ticksPerWheel
func NewTicker(duration time.Duration) *Ticker {
return hourglass.NewTicker(duration)
}
// wheel is a ring queue and length is hourglass.ticksPerWheel
// currentTick is current tick in this wheel
// when currentTick less than hourglass.ticksPerWheel currentTick += 1
// when currentTick equal than hourglass.ticksPerWheel currentTick = 0 and wheel.next.tick()
// tickers is []Ticker for every currentTick
// all ticker in tickers[currentTick] will target in wheel.tick()
type wheel struct {
currentTick int
tickDuration time.Duration
ticksPerWheel int
tickers []map[*Ticker]struct{}
locker sync.Mutex
prev *wheel
next *wheel
}
func (w *wheel) setTicker(ticker *Ticker) {
w.locker.Lock()
defer w.locker.Unlock()
if ticker.closed {
val, ok := ticker.arg.(chan time.Time)
if ok {
close(val)
}
return
}
ticks := int(ticker.duration / w.tickDuration)
if ticks <= w.ticksPerWheel {
//insert into current wheel
tick := (ticks - 1 + w.currentTick) % w.ticksPerWheel
if w.tickers[tick] == nil {
w.tickers[tick] = make(map[*Ticker]struct{}, 8)
}
w.tickers[tick][ticker] = struct{}{}
return
}
//next wheel
if w.next == nil {
w.next = &wheel{
currentTick: w.currentTick,
tickDuration: w.tickDuration * time.Duration(w.ticksPerWheel),
ticksPerWheel: w.ticksPerWheel,
tickers: make([]map[*Ticker]struct{}, w.ticksPerWheel),
locker: sync.Mutex{},
prev: w,
}
}
w.next.setTicker(ticker)
}
func (w *wheel) tick(t time.Time) {
w.locker.Lock()
defer w.locker.Unlock()
for ticker := range w.tickers[w.currentTick] {
go func(tk *Ticker) {
tk.locker.RLock()
defer tk.locker.RUnlock()
tk.f(tk.arg, t)
w.rootWheel().setTicker(tk)
}(ticker)
delete(w.tickers[w.currentTick], ticker)
}
w.currentTick += 1
if w.currentTick == w.ticksPerWheel {
w.currentTick = 0
if w.next != nil {
w.next.tick(t)
}
}
}
func (w *wheel) rootWheel() *wheel {
if w.prev == nil {
return w
}
return w.prev.rootWheel()
}
func formatDuration(tickDuration, duration time.Duration) time.Duration {
if duration%tickDuration != 0 {
duration = duration / tickDuration
}
if duration < tickDuration {
duration = tickDuration
}
return duration
}
func sendTime(c any, t time.Time) {
select {
case c.(chan time.Time) <- t:
default:
}
}
func goFunc(f any, t time.Time) {
go f.(func(time.Time))(t)
}