修复时间轮代码

This commit is contained in:
liwei 2023-07-13 11:36:06 +08:00
parent 0a17fb1517
commit 1c070e2646

View File

@ -88,7 +88,7 @@ func (t *Task) Reset() {
t.circle = false t.circle = false
} }
//启动时间轮 // 启动时间轮
func (this *TimeWheel) Start() { func (this *TimeWheel) Start() {
// onlye once start // onlye once start
this.onceStart.Do( this.onceStart.Do(
@ -114,12 +114,12 @@ func (this *TimeWheel) Remove(task *Task) error {
return nil return nil
} }
//停止时间轮 // 停止时间轮
func (this *TimeWheel) Stop() { func (this *TimeWheel) Stop() {
this.stopC <- struct{}{} this.stopC <- struct{}{}
} }
//此处写法 为监控时间轮是否正常执行 // 此处写法 为监控时间轮是否正常执行
func (this *TimeWheel) tickGenerator() { func (this *TimeWheel) tickGenerator() {
if this.tickQueue == nil { if this.tickQueue == nil {
return return
@ -137,7 +137,7 @@ func (this *TimeWheel) tickGenerator() {
} }
} }
//调度器 // 调度器
func (this *TimeWheel) schduler() { func (this *TimeWheel) schduler() {
queue := this.ticker.C queue := this.ticker.C
if this.tickQueue != nil { if this.tickQueue != nil {
@ -160,7 +160,7 @@ func (this *TimeWheel) schduler() {
} }
} }
//清理 // 清理
func (this *TimeWheel) collectTask(task *Task) { func (this *TimeWheel) collectTask(task *Task) {
index := this.bucketIndexes[task.id] index := this.bucketIndexes[task.id]
delete(this.bucketIndexes, task.id) delete(this.bucketIndexes, task.id)
@ -185,8 +185,9 @@ func (this *TimeWheel) handleTick() {
} }
if task.async { if task.async {
go func(task *Task) { go func(_task *Task) {
go this.calltask(task, task.args...) this.calltask(_task, _task.args...)
this.collectTask(task)
}(task) }(task)
} else { } else {
@ -202,8 +203,10 @@ func (this *TimeWheel) handleTick() {
} }
// gc // gc
if !task.async {
this.collectTask(task) this.collectTask(task)
} }
}
if this.currentIndex == this.bucketsNum-1 { if this.currentIndex == this.bucketsNum-1 {
this.currentIndex = 0 this.currentIndex = 0
@ -213,7 +216,7 @@ func (this *TimeWheel) handleTick() {
this.currentIndex++ this.currentIndex++
} }
//执行时间轮事件 捕捉异常错误 防止程序崩溃 // 执行时间轮事件 捕捉异常错误 防止程序崩溃
func (this *TimeWheel) calltask(task *Task, args ...interface{}) { func (this *TimeWheel) calltask(task *Task, args ...interface{}) {
defer func() { //程序异常 收集异常信息传递给前端显示 defer func() { //程序异常 收集异常信息传递给前端显示
if r := recover(); r != nil { if r := recover(); r != nil {