Skip to content

Commit

Permalink
fix: 修复 timer.Ticker 死锁
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Apr 19, 2024
1 parent dff6faa commit 45024f3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 3 additions & 2 deletions utils/timer/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down
37 changes: 37 additions & 0 deletions utils/timer/ticker_loop_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 45024f3

Please sign in to comment.