From fc00fa9686ac07035b7306bfa0890f4531b15be6 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Wed, 10 Apr 2024 13:34:17 +0200 Subject: [PATCH] WIP --- pkg/icingadb/ha.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/icingadb/ha.go b/pkg/icingadb/ha.go index 9cdf28e14..7936a7f66 100644 --- a/pkg/icingadb/ha.go +++ b/pkg/icingadb/ha.go @@ -154,7 +154,7 @@ func (h *HA) controller() { // expiration time. Therefore, we use a deadline ctx to retry.WithBackoff() in realize() which expires earlier // than our default timeout. // 2) Since we do not want to exit before our default timeout expires, we have to repeat step 1 until it does. - retryTimeout := time.NewTicker(retry.DefaultTimeout) + retryTimeout := time.NewTimer(retry.DefaultTimeout) defer retryTimeout.Stop() for { @@ -179,7 +179,7 @@ func (h *HA) controller() { h.realizeLostHeartbeat() // Reset retry timeout so that the next iterations have the full amount of time available again. - retryTimeout.Reset(retry.DefaultTimeout) + resetTimer(h.ctx, retryTimeout, retry.DefaultTimeout) continue } @@ -251,7 +251,7 @@ func (h *HA) controller() { // But this is the best place to catch all scenarios where the timeout needs to be reset. // And since HA needs quite a bit of refactoring anyway to e.g. return immediately after calling h.abort(), // it's fine to have it here for now. - retryTimeout.Reset(retry.DefaultTimeout) + resetTimer(h.ctx, retryTimeout, retry.DefaultTimeout) case <-h.heartbeat.Done(): if err := h.heartbeat.Err(); err != nil { h.abort(err) @@ -528,3 +528,14 @@ func (h *HA) signalTakeover(reason string) { } } } + +func resetTimer(ctx context.Context, t *time.Timer, d time.Duration) { + if !t.Stop() { + select { + case <-t.C: + case <-ctx.Done(): + return + } + } + t.Reset(d) +}