From b57fd415fe7cf2974841a63aa9bb490c6637eb2e Mon Sep 17 00:00:00 2001 From: SubProblem Date: Wed, 15 Apr 2026 00:20:59 +0400 Subject: [PATCH] run health check immediately on startup to avoid dropping connections in first interval --- balancer/health.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/balancer/health.go b/balancer/health.go index b9686e4..e2b4d59 100644 --- a/balancer/health.go +++ b/balancer/health.go @@ -7,21 +7,26 @@ import ( ) func StartHealthChecker(backends []Backend, interval time.Duration) { + checkAll(backends) ticker := time.NewTicker(interval) go func() { defer ticker.Stop() for range ticker.C { - for i := range backends { - conn, err := net.DialTimeout("tcp", backends[i].Address, 2*time.Second) - if err != nil { - log.Printf("Health check failed for %s: %v", backends[i].Address, err) - backends[i].Healthy.Store(false) - } else { - conn.Close() - backends[i].Healthy.Store(true) - } - } + checkAll(backends) } }() +} + +func checkAll(backends []Backend) { + for i := range backends { + conn, err := net.DialTimeout("tcp", backends[i].Address, 2*time.Second) + if err != nil { + log.Printf("Health check failed for %s: %v", backends[i].Address, err) + backends[i].Healthy.Store(false) + } else { + conn.Close() + backends[i].Healthy.Store(true) + } + } } \ No newline at end of file