aceld / zinx

A lightweight concurrent server framework based on Golang.
https://github.com/aceld/zinx/wiki
MIT License
7.07k stars 1.21k forks source link

timerscheduler的CancelTimer,ids有可能越界 #85

Closed zhangxa closed 1 year ago

zhangxa commented 3 years ago

//删除timer func (this *TimerScheduler) CancelTimer(tid uint32) { this.Lock() this.Unlock() //this.tw.RemoveTimer(tid) 这个方法无效 //删除timerId var index = -1 //此处的0可修改为-1 for i := 0; i < len(this.ids); i++ { if this.ids[i] == tid { index = i } } //此处加个判断 if index > -1 { this.ids = append(this.ids[:index], this.ids[index+1:]...) }

}

aceld commented 3 years ago

感谢提供PR,已经修正。https://github.com/aceld/zinx/blob/master/ztimer/timerscheduler.go

//删除timer
func (this *TimerScheduler) CancelTimer(tid uint32) {
    this.Lock()
    this.Unlock()
    //this.tw.RemoveTimer(tid)  这个方法无效
    //删除timerId
    var index = -1
    for i := 0; i < len(this.ids); i++ {
        if this.ids[i] == tid {
            index = i
        }
    }

    if index > -1 {
        this.ids = append(this.ids[:index], this.ids[index+1:]...)
    }
}
me10001 commented 3 years ago

this.Unlock() //此处是不是少了 defer ?

//删除timer
func (this *TimerScheduler) CancelTimer(tid uint32) {
    this.Lock()
    this.Unlock()   //此处是不是少了 defer  ?
    //this.tw.RemoveTimer(tid)  这个方法无效
    //删除timerId
    var index = -1
    for i := 0; i < len(this.ids); i++ {
        if this.ids[i] == tid {
            index = i
        }
    }

    if index > -1 {
        this.ids = append(this.ids[:index], this.ids[index+1:]...)
    }
}
zhangxa commented 3 years ago

@me10001 @aceld 确实是少了defer

aceld commented 1 year ago

已更正。