diff --git a/cmd/root.go b/cmd/root.go index 9eb313752b621e97588cc165c054bd02b907c91d..e283842b3b740ce5732f99c2a6313a4f25c14c72 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 37f20943e8a4417dfe7fafc9d3f4e0facf86cc7a..31f9034202a4533cb4064b6cc526111ca375b457 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 99c46a88cb43c90cbe53853b4314eaaa8d22383d..f8fb2c8250265baec3bc7e934cb02115857de952 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 {