Skip to content

Commit

Permalink
we should drain timer before reset it
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmood committed Jul 12, 2024
1 parent c670738 commit 01a50b9
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (de *IdleCounter) Done() {

de.job--
if de.job == 0 {
de.tmr.Reset(de.duration)
de.reset(de.duration)
}
if de.job < 0 {
panic("all jobs are already done")
Expand All @@ -218,7 +218,7 @@ func (de *IdleCounter) Done() {
func (de *IdleCounter) Wait(ctx context.Context) {
de.lock.Lock()
if de.job == 0 {
de.tmr.Reset(de.duration)
de.reset(de.duration)
}
de.lock.Unlock()

Expand All @@ -229,6 +229,16 @@ func (de *IdleCounter) Wait(ctx context.Context) {
}
}

func (de *IdleCounter) reset(d time.Duration) {
if !de.tmr.Stop() {
select {
case <-de.tmr.C:
default:
}
}
de.tmr.Reset(d)
}

var chPause = make(chan struct{})

// Pause the goroutine forever.
Expand Down

0 comments on commit 01a50b9

Please sign in to comment.