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