Skip to content

Commit f3de2a2

Browse files
committed
修改
1 parent 960cf9b commit f3de2a2

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

time_wheel.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ type timeWheel struct {
4646
}
4747

4848
func newTimeWheel() *timeWheel {
49-
5049
ctx, cancel := context.WithCancel(context.Background())
5150

5251
t := &timeWheel{ctx: ctx, cancel: cancel}
@@ -57,10 +56,8 @@ func newTimeWheel() *timeWheel {
5756
}
5857

5958
func (t *timeWheel) init() {
60-
6159
for i := 0; i < nearSize; i++ {
6260
t.t1[i] = newTimeHead(1, uint64(i))
63-
6461
}
6562

6663
for i := 0; i < 4; i++ {
@@ -69,7 +66,7 @@ func (t *timeWheel) init() {
6966
}
7067
}
7168

72-
t.curTimePoint = get10Ms()
69+
// t.curTimePoint = get10Ms()
7370
}
7471

7572
func maxVal() uint64 {
@@ -85,7 +82,6 @@ func (t *timeWheel) index(n int) uint64 {
8582
}
8683

8784
func (t *timeWheel) add(node *timeNode, jiffies uint64) *timeNode {
88-
8985
var head *Time
9086
expire := node.expire
9187
idx := expire - jiffies
@@ -126,7 +122,6 @@ func (t *timeWheel) add(node *timeNode, jiffies uint64) *timeNode {
126122
}
127123

128124
func (t *timeWheel) AfterFunc(expire time.Duration, callback func()) TimeNoder {
129-
130125
jiffies := atomic.LoadUint64(&t.jiffies)
131126

132127
expire = expire/(time.Millisecond*10) + time.Duration(jiffies)
@@ -144,7 +139,6 @@ func getExpire(expire time.Duration, jiffies uint64) time.Duration {
144139
}
145140

146141
func (t *timeWheel) ScheduleFunc(userExpire time.Duration, callback func()) TimeNoder {
147-
148142
jiffies := atomic.LoadUint64(&t.jiffies)
149143

150144
expire := getExpire(userExpire, jiffies)
@@ -165,7 +159,6 @@ func (t *timeWheel) Stop() {
165159

166160
// 移动链表
167161
func (t *timeWheel) cascade(levelIndex int, index int) {
168-
169162
tmp := newTimeHead(0, 0)
170163

171164
l := t.t2Tot5[levelIndex][index]
@@ -186,22 +179,20 @@ func (t *timeWheel) cascade(levelIndex int, index int) {
186179
node := (*timeNode)(pos.Entry(offset))
187180
t.add(node, atomic.LoadUint64(&t.jiffies))
188181
})
189-
190182
}
191183

192184
// moveAndExec函数功能
193-
//1. 先移动到near链表里面
194-
//2. near链表节点为空时,从上一层里面移动一些节点到下一层
195-
//3. 再执行
185+
// 1. 先移动到near链表里面
186+
// 2. near链表节点为空时,从上一层里面移动一些节点到下一层
187+
// 3. 再执行
196188
func (t *timeWheel) moveAndExec() {
197-
198189
// 这里时间溢出
199190
if uint32(t.jiffies) == 0 {
200191
// TODO
201192
// return
202193
}
203194

204-
//如果本层的盘子没有定时器,这时候从上层的盘子移动一些过来
195+
// 如果本层的盘子没有定时器,这时候从上层的盘子移动一些过来
205196
index := t.jiffies & nearMask
206197
if index == 0 {
207198
for i := 0; i <= 3; i++ {
@@ -249,7 +240,6 @@ func (t *timeWheel) moveAndExec() {
249240
t.add(val, jiffies)
250241
}
251242
})
252-
253243
}
254244

255245
// get10Ms函数通过参数传递,为了方便测试
@@ -275,7 +265,6 @@ func (t *timeWheel) run(get10Ms func() time.Duration) {
275265
for i := 0; i < int(diff); i++ {
276266
t.moveAndExec()
277267
}
278-
279268
}
280269

281270
// 自定义, TODO
@@ -284,7 +273,7 @@ func (t *timeWheel) CustomFunc(n Next, callback func()) TimeNoder {
284273
}
285274

286275
func (t *timeWheel) Run() {
287-
276+
t.curTimePoint = get10Ms()
288277
// 10ms精度
289278
tk := time.NewTicker(time.Millisecond * 10)
290279
defer tk.Stop()

0 commit comments

Comments
 (0)