Skip to content

Commit 01a50b9

Browse files
committed
we should drain timer before reset it
According to https://antonz.org/timer-reset
1 parent c670738 commit 01a50b9

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

lib/utils/utils.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (de *IdleCounter) Done() {
207207

208208
de.job--
209209
if de.job == 0 {
210-
de.tmr.Reset(de.duration)
210+
de.reset(de.duration)
211211
}
212212
if de.job < 0 {
213213
panic("all jobs are already done")
@@ -218,7 +218,7 @@ func (de *IdleCounter) Done() {
218218
func (de *IdleCounter) Wait(ctx context.Context) {
219219
de.lock.Lock()
220220
if de.job == 0 {
221-
de.tmr.Reset(de.duration)
221+
de.reset(de.duration)
222222
}
223223
de.lock.Unlock()
224224

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

232+
func (de *IdleCounter) reset(d time.Duration) {
233+
if !de.tmr.Stop() {
234+
select {
235+
case <-de.tmr.C:
236+
default:
237+
}
238+
}
239+
de.tmr.Reset(d)
240+
}
241+
232242
var chPause = make(chan struct{})
233243

234244
// Pause the goroutine forever.

0 commit comments

Comments
 (0)