Skip to content

Commit

Permalink
fix: 修复 timer.Ticker 并发问题
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Mar 11, 2024
1 parent 17cdad2 commit d1d5bd4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 4 additions & 2 deletions utils/timer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package timer
type Option func(ticker *Ticker)

// WithCaller 通过其他的 handler 执行 Caller
func WithCaller(handle func(name string, caller func())) Option {
func WithCaller(handler func(name string, caller func())) Option {
return func(ticker *Ticker) {
ticker.handler = handle
ticker.lock.Lock()
ticker.handler = handler
ticker.lock.Unlock()
}
}

Expand Down
6 changes: 5 additions & 1 deletion utils/timer/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,11 @@ func (slf *Ticker) loop(name string, after, interval time.Duration, expr *cronex
slf.timers[name] = scheduler
if slf.handler != nil {
scheduler.timer = slf.wheel.ScheduleFunc(scheduler, func() {
slf.handler(scheduler.Name(), scheduler.Caller)
slf.lock.RLock()
defer slf.lock.RUnlock()
if slf.handler != nil {
slf.handler(scheduler.Name(), scheduler.Caller)
}
})
} else {
scheduler.timer = slf.wheel.ScheduleFunc(scheduler, scheduler.Caller)
Expand Down

0 comments on commit d1d5bd4

Please sign in to comment.