From 8d2f26110567af9575b568ff2c471a0d99abad17 Mon Sep 17 00:00:00 2001 From: jebbs Date: Mon, 17 Oct 2022 10:43:46 +0800 Subject: [PATCH] fix health check ticker interval and close --- balancer/healthcheck.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/balancer/healthcheck.go b/balancer/healthcheck.go index c636bd31..da54bc34 100644 --- a/balancer/healthcheck.go +++ b/balancer/healthcheck.go @@ -27,6 +27,8 @@ type HealthCheck struct { options *option.HealthCheckSettings results map[string]*result + + close chan struct{} } type result struct { @@ -73,15 +75,16 @@ func (h *HealthCheck) Start() error { if h.ticker != nil { return nil } - interval := time.Duration(h.options.Interval) * time.Duration(h.options.SamplingCount) - ticker := time.NewTicker(interval) + h.close = make(chan struct{}) + ticker := time.NewTicker(time.Duration(h.options.Interval)) h.ticker = ticker go func() { for { - h.CheckNodes() - _, ok := <-ticker.C - if !ok { - break + select { + case <-h.close: + return + case <-ticker.C: + h.CheckNodes() } } }() @@ -96,6 +99,7 @@ func (h *HealthCheck) Close() error { h.ticker.Stop() h.ticker = nil } + close(h.close) return nil }