Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions timingwheel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"github.com/RussellLuo/timingwheel/delayqueue"
)

var taskRunner = func(task func()) {
// Like the standard time.AfterFunc (https://golang.org/pkg/time/#AfterFunc),
// always execute the timer's task in its own goroutine.
go task()
}

// TimingWheel is an implementation of Hierarchical Timing Wheels.
type TimingWheel struct {
tick int64 // in milliseconds
Expand Down Expand Up @@ -111,10 +117,7 @@ func (tw *TimingWheel) add(t *Timer) bool {
func (tw *TimingWheel) addOrRun(t *Timer) {
if !tw.add(t) {
// Already expired

// Like the standard time.AfterFunc (https://golang.org/pkg/time/#AfterFunc),
// always execute the timer's task in its own goroutine.
go t.task()
taskRunner(t.task)
}
}

Expand Down Expand Up @@ -224,3 +227,8 @@ func (tw *TimingWheel) ScheduleFunc(s Scheduler, f func()) (t *Timer) {

return
}

// SetTaskRunner replace default timer execute function.
func SetTaskRunner(runner func(task func())) {
taskRunner = runner
}