From 64f8e79bc71256bf9e5f20b4256d1f70c68d82e2 Mon Sep 17 00:00:00 2001 From: Klaus Post Date: Thu, 5 Dec 2024 08:53:45 -0800 Subject: [PATCH] Add health check jitter (#122) Prevent health checks to sync up, by adding 100% jitter to each call. --- main.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 578cecf..8636c6a 100644 --- a/main.go +++ b/main.go @@ -313,6 +313,7 @@ func getHealthCheckURL(endpoint, healthCheckPath string, healthCheckPort int) (s // healthCheck - background routine which checks if a backend is up or down. func (b *Backend) healthCheck(ctxt context.Context) { + rng := rand.New(rand.NewSource(time.Now().UnixNano())) timer := time.NewTimer(b.healthCheckDuration) defer timer.Stop() for { @@ -324,7 +325,8 @@ func (b *Backend) healthCheck(ctxt context.Context) { if err != nil { console.Errorln(err) } - timer.Reset(b.healthCheckDuration) + // Add random jitter to call + timer.Reset(b.healthCheckDuration + time.Duration(rng.Int63n(int64(b.healthCheckDuration)))) } } } @@ -578,7 +580,7 @@ func (s *site) nextProxy() (*Backend, func()) { } switch globalHostBalance { case "least": - min := int64(math.MaxInt64) + minCall := int64(math.MaxInt64) earliest := int64(math.MaxInt64) idx := 0 // Shuffle before picking the least connection to ensure all nodes @@ -588,8 +590,8 @@ func (s *site) nextProxy() (*Backend, func()) { }) for i, backend := range backends { currentCalls := backend.Stats.CurrentCalls.Load() - if currentCalls < min { - min = currentCalls + if currentCalls < minCall { + minCall = currentCalls lastFinished := backend.Stats.LastFinished.Load() if lastFinished < earliest { earliest = lastFinished