fix: 修复 timer.Ticker 死锁

This commit is contained in:
kercylan98 2024-04-19 15:10:00 +08:00
parent dff6faa834
commit 45024f3b9f
2 changed files with 40 additions and 2 deletions

View File

@ -151,8 +151,9 @@ func (slf *Ticker) loop(name string, after, interval time.Duration, expr *cronex
if slf.handler != nil {
scheduler.timer = slf.wheel.ScheduleFunc(scheduler, func() {
slf.lock.RLock()
defer slf.lock.RUnlock()
if slf.handler != nil {
handler := slf.handler
slf.lock.RUnlock()
if handler != nil {
slf.handler(scheduler.Name(), scheduler.Caller)
}
})

View File

@ -0,0 +1,37 @@
package timer
import (
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/kercylan98/minotaur/utils/times"
"testing"
"time"
)
func TestTicker_Loop(t *testing.T) {
r := gin.Default()
pprof.Register(r)
go func() {
r.Run(":9999")
}()
ticker := GetTicker(10, WithCaller(func(name string, caller func()) {
caller()
}))
ticker.After("stop", time.Second, func() {
ticker.StopTimer("stop")
t.Log("success")
ticker.After("stop1", time.Second, func() {
ticker.StopTimer("stop1")
t.Log("success1")
ticker.After("stop2", time.Second, func() {
ticker.StopTimer("stop2")
t.Log("success2")
})
})
})
time.Sleep(times.Week)
}