From 9c0fccf7839d09e1ff5e756bfabb7c13f6b6aa2e Mon Sep 17 00:00:00 2001 From: Crystal Lemire Date: Mon, 11 Dec 2023 11:06:30 -0800 Subject: [PATCH] Move all writes to the mutable state timer to be behind a lock in order to elimiate false positives in race tests. --- protocol/daemons/server/types/health_checker.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/protocol/daemons/server/types/health_checker.go b/protocol/daemons/server/types/health_checker.go index 16e6f0b900..c386acfc96 100644 --- a/protocol/daemons/server/types/health_checker.go +++ b/protocol/daemons/server/types/health_checker.go @@ -112,6 +112,20 @@ func (u *healthCheckerMutableState) SchedulePoll(nextPollDelay time.Duration) { u.timer.Reset(nextPollDelay) } +// InitializePolling schedules the first poll for the health-checkable service. This method is synchronized. +func (u *healthCheckerMutableState) InitializePolling(firstPollDelay time.Duration, pollFunc func()) { + u.lock.Lock() + defer u.lock.Unlock() + + // If the timer is already initialized, don't initialize it again. + if u.timer != nil { + return + } + + // The first poll is scheduled after a custom delay to allow the service to initialize. + u.timer = time.AfterFunc(firstPollDelay, pollFunc) +} + // Stop stops the health checker. This method is synchronized. func (u *healthCheckerMutableState) Stop() { u.lock.Lock() @@ -204,7 +218,7 @@ func StartNewHealthChecker( } // The first poll is scheduled after the startup grace period to allow the service to initialize. - checker.mutableState.timer = time.AfterFunc(startupGracePeriod, checker.Poll) + checker.mutableState.InitializePolling(startupGracePeriod, checker.Poll) return checker }