cleanup: prevent leaks from time.After

This PR replaces use of time.After with a safe helper function
that creates a time.Timer to use instead. The new function returns
both a time.Timer and a Stop function that the caller must handle.

Unlike time.NewTimer, the helper function does not panic if the duration
set is <= 0.
This commit is contained in:
Seth Hoenig
2022-02-02 10:59:53 -06:00
parent c613dc5d2c
commit c1e033c8c6
16 changed files with 144 additions and 21 deletions

View File

@@ -527,6 +527,9 @@ func (tr *TaskRunner) Run() {
return
}
timer, stop := helper.NewSafeTimer(0) // timer duration calculated JIT
defer stop()
MAIN:
for !tr.shouldShutdown() {
select {
@@ -612,9 +615,11 @@ MAIN:
break MAIN
}
timer.Reset(restartDelay)
// Actually restart by sleeping and also watching for destroy events
select {
case <-time.After(restartDelay):
case <-timer.C:
case <-tr.killCtx.Done():
tr.logger.Trace("task killed between restarts", "delay", restartDelay)
break MAIN