golang1.23版本之前 Timer Reset方法無法正確使用
golang1.23 之前 Reset ?到底有什么問題
在 golang 的 time.Reset 文檔中有這么一句話,為了防止文檔更新而導致內容變動,這里粘貼出來:
Before Go 1.23, the only safe way to use Reset was to [Stop] and explicitly drain the timer first. See the NewTimer documentation for more details. 在Go 1.23 之前,唯一安全使用Reset函數(shù)的方式是:在使用之前調用Stop函數(shù)并且明確的從timer的channel中抽取出東西。
雖然文檔中已經告訴了正確使用的方式,但是實際上在真正的代碼中無法達到這個要求,參考下方代碼(來源代碼來源):
//consumer
go func() {
// try to read from channel, block at most 5s.
// if timeout, print time event and go on loop.
// if read a message which is not the type we want(we want true, not false),
// retry to read.
timer := time.NewTimer(time.Second * 5)
for {
// timer may be not active, and fired
if !timer.Stop() {
select {
case <-timer.C: //try to drain from the channel,嘗試抽取,由于使用select,因此這里可以保證:不阻塞 + 一定抽取成功
default:
}
}
timer.Reset(time.Second * 5) //重置定時器
select {
case b := <-c:
if b == false {
fmt.Println(time.Now(), ":recv false. continue")
continue
}
//we want true, not false
fmt.Println(time.Now(), ":recv true. return")
return
case <-timer.C:
fmt.Println(time.Now(), ":timer expired")
continue
}
}
}()
在上面的代碼中,我們按照文檔的要求,在 timer.Reset ?之前已經調用了 Stop ?函數(shù),且如果 Stop 成功(返回 true),還嘗試抽取 timer,看起來似乎沒問題的代碼中仍然存在問題。
問題的關鍵在于:當 Ticket 觸發(fā)的時候,設置定時器狀態(tài)的操作和發(fā)送 channel 的操作并不是原子的,見 runOneTimer 函數(shù)。
異常情況:嘗試抽取 channel 在 發(fā)送 channel 之前,這樣會導致 Reset 之前并沒有真正的清空 timer,后一次的 timer.C 還沒到觸發(fā)時間就異常的觸發(fā)了!

golang1.23 之前到底應該如何正確的使用 Reset?
實際上簡潔點就這么寫,每次一個新的局部變量 Timer? 結構體沒壓力,非要復用使用 Reset 的可讀性太差了,對維護者不友好,而且習慣了不好的寫法,哪天一不小心就寫出問題了~
go func() {
for {
func() {
timer := time.NewTimer(time.Second * 2)
defer timer.Stop()
select {
case b := <-c:
if !b {
fmt.Println(time.Now(), "work...")
}
case <-timer.C: // BBB: normal receive from channel timeout event
fmt.Println(time.Now(), "timeout")
}
}()
}
}()
參考:
https://tonybai.com/2016/12/21/how-to-use-timer-reset-in-golang-correctly/
到此這篇關于golang1.23版本之前 Timer Reset方法無法正確使用的文章就介紹到這了,更多相關golang Timer Reset方法無法正確使用內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Golang中json和jsoniter的區(qū)別使用示例
這篇文章主要介紹了Golang中json和jsoniter的區(qū)別使用示例,本文給大家分享兩種區(qū)別,結合示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2023-12-12
golang實現(xiàn)簡易的分布式系統(tǒng)方法
這篇文章主要介紹了golang實現(xiàn)簡易的分布式系統(tǒng)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10
淺析Golang切片截取功能與C++的vector區(qū)別
這篇文章主要介紹了Golang中切片的截取功能與C++中的vector有什么區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-10-10

