所谓陷阱,就是它不是你认为的那样,这种认知偏差可能让你的软件留下隐藏Bug。恰好Timer就有3个陷阱,咱们会讲 1)Reset的陷阱和 2)通道的陷阱,3)Stop的陷阱与Reset的陷阱相似,本身探索吧。缓存
Timer.Reset()
函数的返回值是bool类型,咱们看一个问题三连:函数
Reset的返回值是否是这个意思?测试
<!--more-->ui
经过查看文档和实现,Timer.Reset()
的返回值并不符合咱们的预期,这就是偏差。它的返回值不表明重设定时器成功或失败,而是在表达定时器在重设前的状态:code
因此,当Reset返回false时,咱们并不能认为一段时间以后,超时不会到来,实际上可能会到来,定时器已经生效了。协程
如何跳过前面的陷阱,让Reset符合咱们的预期功能呢?直接忽视Reset的返回值好了,它不能帮助你达到预期的效果。事件
真正的陷阱是Timer的通道,它和咱们预期的成功、失败密切相关。咱们所指望的定时器设置失败,一般只和通道有关:设置定时器前,定时器的通道Timer.C
中是否已经有数据。文档
接下来解释为什么失败只与通道中是否存在超时事件有关。get
定时器的缓存通道大小只为1,没法多存放超时事件,看源码。源码
// NewTimer creates a new Timer that will send // the current time on its channel after at least duration d. func NewTimer(d Duration) *Timer { c := make(chan Time, 1) // 缓存通道大小为1 t := &Timer{ C: c, r: runtimeTimer{ when: when(d), f: sendTime, arg: c, }, } startTimer(&t.r) return t }
定时器建立后是单独运行的,超时后会向通道写入数据,你从通道中把数据读走。当前一次的超时数据没有被读取,而设置了新的定时器,而后去通道读数据,结果读到的是上次超时的超时事件,看似成功,实则失败,彻底掉入陷阱。
若是确保Timer.Reset()
成功,获得咱们想要的结果?Timer.Reset()
前清空通道。
// 方法1 if len(Timer.C) > 0{ <-Timer.C } Timer.Reset(time.Second)
通过和@周志荣_9447的讨论和思考,更加合理的作法仍是下面这样:
// 方法2 if !Timer.Stop() && len(Timer.C) > 0{ <-Timer.C } Timer.Reset(time.Second)
定时器的运行和len(Timer.C)
的判断是在不一样的协程中,当判断的时候通道大小可能为0,但当执行Reset()
的前的这段时间,旧的定时器超时,通道中存在超时时间,再执行Reset()
也达不到预期的效果。
方法2才是合理的方法。先执行Stop()
,能够确保旧定时器已经中止,不会再向通道中写入超时事件,就可解决上面的问题。Stop()
返回false并非表明,通道中必定存在超时事件,因此还需使用len(Timer.C) > 0
进行判断再决定是否清空通道。
package main import ( "fmt" "time" ) // 不一样状况下,Timer.Reset()的返回值 func test1() { fmt.Println("第1个测试:Reset返回值和什么有关?") tm := time.NewTimer(time.Second) defer tm.Stop() quit := make(chan bool) // 退出事件 go func() { time.Sleep(3 * time.Second) quit <- true }() // Timer未超时,看Reset的返回值 if !tm.Reset(time.Second) { fmt.Println("未超时,Reset返回false") } else { fmt.Println("未超时,Reset返回true") } // 中止timer tm.Stop() if !tm.Reset(time.Second) { fmt.Println("中止Timer,Reset返回false") } else { fmt.Println("中止Timer,Reset返回true") } // Timer超时 for { select { case <-quit: return case <-tm.C: if !tm.Reset(time.Second) { fmt.Println("超时,Reset返回false") } else { fmt.Println("超时,Reset返回true") } } } } func test2() { fmt.Println("\n第2个测试:超时后,不读通道中的事件,能够Reset成功吗?") sm2Start := time.Now() tm2 := time.NewTimer(time.Second) time.Sleep(2 * time.Second) fmt.Printf("Reset前通道中事件的数量:%d\n", len(tm2.C)) if !tm2.Reset(time.Second) { fmt.Println("不读通道数据,Reset返回false") } else { fmt.Println("不读通道数据,Reset返回true") } fmt.Printf("Reset后通道中事件的数量:%d\n", len(tm2.C)) select { case t := <-tm2.C: fmt.Printf("tm2开始的时间: %v\n", sm2Start.Unix()) fmt.Printf("通道中事件的时间:%v\n", t.Unix()) if t.Sub(sm2Start) <= time.Second+time.Millisecond { fmt.Println("通道中的时间是从新设置sm2前的时间,即第一次超时的时间,因此第二次Reset失败了") } } fmt.Printf("读通道后,其中事件的数量:%d\n", len(tm2.C)) tm2.Reset(time.Second) fmt.Printf("再次Reset后,通道中事件的数量:%d\n", len(tm2.C)) time.Sleep(2 * time.Second) fmt.Printf("超时后通道中事件的数量:%d\n", len(tm2.C)) } func test3() { fmt.Println("\n第3个测试:Reset前清空通道,尽量通畅") smStart := time.Now() tm := time.NewTimer(time.Second) time.Sleep(2 * time.Second) // 停掉定时器再清空 if !tm.Stop() && len(tm.C) > 0 { <-tm.C } tm.Reset(time.Second) // 超时 t := <-tm.C fmt.Printf("tm开始的时间: %v\n", smStart.Unix()) fmt.Printf("通道中事件的时间:%v\n", t.Unix()) if t.Sub(smStart) <= time.Second+time.Millisecond { fmt.Println("通道中的时间是从新设置sm前的时间,即第一次超时的时间,因此第二次Reset失败了") } else { fmt.Println("通道中的时间是从新设置sm后的时间,Reset成功了") } } func main() { test1() test2() test3() }
若是这篇文章对你有帮助,请点个赞/喜欢,让我知道个人写做是有价值的,感谢。