Skip to content

Commit 64f8e79

Browse files
authored
Add health check jitter (#122)
Prevent health checks to sync up, by adding 100% jitter to each call.
1 parent 33b8516 commit 64f8e79

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

main.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ func getHealthCheckURL(endpoint, healthCheckPath string, healthCheckPort int) (s
313313

314314
// healthCheck - background routine which checks if a backend is up or down.
315315
func (b *Backend) healthCheck(ctxt context.Context) {
316+
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
316317
timer := time.NewTimer(b.healthCheckDuration)
317318
defer timer.Stop()
318319
for {
@@ -324,7 +325,8 @@ func (b *Backend) healthCheck(ctxt context.Context) {
324325
if err != nil {
325326
console.Errorln(err)
326327
}
327-
timer.Reset(b.healthCheckDuration)
328+
// Add random jitter to call
329+
timer.Reset(b.healthCheckDuration + time.Duration(rng.Int63n(int64(b.healthCheckDuration))))
328330
}
329331
}
330332
}
@@ -578,7 +580,7 @@ func (s *site) nextProxy() (*Backend, func()) {
578580
}
579581
switch globalHostBalance {
580582
case "least":
581-
min := int64(math.MaxInt64)
583+
minCall := int64(math.MaxInt64)
582584
earliest := int64(math.MaxInt64)
583585
idx := 0
584586
// Shuffle before picking the least connection to ensure all nodes
@@ -588,8 +590,8 @@ func (s *site) nextProxy() (*Backend, func()) {
588590
})
589591
for i, backend := range backends {
590592
currentCalls := backend.Stats.CurrentCalls.Load()
591-
if currentCalls < min {
592-
min = currentCalls
593+
if currentCalls < minCall {
594+
minCall = currentCalls
593595
lastFinished := backend.Stats.LastFinished.Load()
594596
if lastFinished < earliest {
595597
earliest = lastFinished

0 commit comments

Comments
 (0)