From 6df18a88aeaeeb28973269150b191a6263e20f84 Mon Sep 17 00:00:00 2001 From: "Richard T. Carback III" <rick.carback@gmail.com> Date: Fri, 2 Oct 2020 19:10:33 +0000 Subject: [PATCH] Fix health tracker timer logic --- cmd/root.go | 2 +- network/health/tracker.go | 20 ++++++++++++++------ network/health/tracker_test.go | 6 +++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 9eb313752..e283842b3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -168,7 +168,7 @@ var rootCmd = &cobra.Command{ for !isConnected { select { case isConnected = <-connected: - jww.INFO.Printf("health status: %b\n", + jww.INFO.Printf("health status: %v\n", isConnected) break case <-timeoutTick.C: diff --git a/network/health/tracker.go b/network/health/tracker.go index 37f20943e..31f903420 100644 --- a/network/health/tracker.go +++ b/network/health/tracker.go @@ -113,9 +113,7 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) { // Long-running thread used to monitor and report on network health func (t *Tracker) start(quitCh <-chan struct{}) { - - var timerChan <-chan time.Time - timerChan = make(chan time.Time) + timer := time.NewTimer(t.timeout) for { var heartbeat network.Heartbeat @@ -124,13 +122,23 @@ func (t *Tracker) start(quitCh <-chan struct{}) { // Handle thread kill break case heartbeat = <-t.heartbeat: - jww.INFO.Printf("heartbeat: %+v", heartbeat) + jww.INFO.Printf("heartbeat: %v", heartbeat) + // Stop and reset timer + if !timer.Stop() { + select { + case <-timer.C: // per docs explicitly drain + default: + } + } + timer.Reset(t.timeout) if healthy(heartbeat) { - timerChan = time.NewTimer(t.timeout).C t.setHealth(true) } - case <-timerChan: + break + case <-timer.C: t.setHealth(false) + timer.Reset(t.timeout) + break } } } diff --git a/network/health/tracker_test.go b/network/health/tracker_test.go index 99c46a88c..f8fb2c825 100644 --- a/network/health/tracker_test.go +++ b/network/health/tracker_test.go @@ -16,9 +16,9 @@ import ( // Happy path smoke test func TestNewTracker(t *testing.T) { // Initialize required variables - timeout := 500 * time.Millisecond + timeout := 250 * time.Millisecond tracker := newTracker(timeout) - counter := 0 + counter := 2 // First signal is "false/unhealthy" positiveHb := network.Heartbeat{ HasWaitingRound: true, IsRoundComplete: true, @@ -57,7 +57,7 @@ func TestNewTracker(t *testing.T) { tracker.heartbeat <- positiveHb // Wait for the heartbeat to register - for i := 0; i < 5; i++ { + for i := 0; i < 4; i++ { if tracker.IsHealthy() && counter == expectedCount { break } else { -- GitLab