Skip to content

Commit

Permalink
Add health check jitter (#122)
Browse files Browse the repository at this point in the history
Prevent health checks to sync up, by adding 100% jitter to each call.
  • Loading branch information
klauspost authored Dec 5, 2024
1 parent 33b8516 commit 64f8e79
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))))
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 64f8e79

Please sign in to comment.