diff --git a/timedsched.go b/timedsched.go index e20fc52..2db7c20 100644 --- a/timedsched.go +++ b/timedsched.go @@ -62,6 +62,7 @@ func NewTimedSched(parallel int) *TimedSched { func (ts *TimedSched) sched() { var tasks timedFuncHeap timer := time.NewTimer(0) + drained := false for { select { case task := <-ts.chTask: @@ -71,15 +72,22 @@ func (ts *TimedSched) sched() { task.execute() } else { heap.Push(&tasks, task) - // reset timer to trigger based on the top element + // properly reset timer to trigger based on the top element + stopped := timer.Stop() + if !stopped && !drained { + <-timer.C + } timer.Reset(tasks[0].ts.Sub(now)) + drained = false } case now := <-timer.C: + drained = true for tasks.Len() > 0 { if now.After(tasks[0].ts) { heap.Pop(&tasks).(timedFunc).execute() } else { timer.Reset(tasks[0].ts.Sub(now)) + drained = false break } }