From 6c305d98e24f9d3fb8fdd02fe3cd39a82099ec06 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 14 Jan 2022 15:15:08 -0600 Subject: [PATCH] [Heartbeat] Use timer.reset now that golang has been updated (#29729) (#29733) Undoes https://github.com/elastic/beats/pull/27006/files (while preserving the new test), and also cleaning up the syntax using `time.Until`. Since the The golang bug I reported in https://github.com/golang/go/issues/47329 has been fixed since somewhere in go 1.16.x (it's hard to track the exact version). This should be backported to 7.16.x since that already uses go 1.17.x and is safe. (cherry picked from commit 7b095dae991763ab612d673f85fcbec920b2fb53) Co-authored-by: Andrew Cholakian --- heartbeat/scheduler/timerqueue/queue.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/heartbeat/scheduler/timerqueue/queue.go b/heartbeat/scheduler/timerqueue/queue.go index 61d4e0ac933d..760dd353f38a 100644 --- a/heartbeat/scheduler/timerqueue/queue.go +++ b/heartbeat/scheduler/timerqueue/queue.go @@ -88,7 +88,7 @@ func (tq *TimerQueue) Start() { if tq.th.Len() > 0 { nr := tq.th[0].runAt tq.nextRunAt = &nr - tq.timer = time.NewTimer(time.Until(nr)) + tq.timer.Reset(time.Until(nr)) } else { tq.timer.Stop() tq.nextRunAt = nil @@ -107,18 +107,7 @@ func (tq *TimerQueue) pushInternal(tt *timerTask) { if tq.nextRunAt != nil && !tq.timer.Stop() { <-tq.timer.C } - // Originally the line below this comment was - // - // tq.timer.Reset(time.Until(tt.runAt)) - // - // however this broke in go1.16rc1, specifically on the commit b4b014465216790e01aa66f9120d03230e4aff46 - //, specifically on this line: - // https://github.com/golang/go/commit/b4b014465216790e01aa66f9120d03230e4aff46#diff-73699b6edfe5dbb3f6824e66bb3566bce9405e9a8c810cac55c8199459f0ac19R652 - // where some nice new optimizations don't actually work reliably - // This can be worked around by instantiating a new timer rather than resetting the timer. - // since that internally calls deltimer in runtime/timer.go rather than modtimer, - // I suspect that the problem is in modtimer's setting of &pp.timerModifiedEarliest - tq.timer = time.NewTimer(time.Until(tt.runAt)) + tq.timer.Reset(time.Until(tt.runAt)) tq.nextRunAt = &tt.runAt } }