From 767eb5688c4327f3280aa2cc93cbb85d82caa9d1 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Fri, 19 Jul 2024 16:39:11 +0200 Subject: [PATCH] fix in consul discovery --- consul/discovery.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/consul/discovery.go b/consul/discovery.go index 68fa11c..b446c14 100644 --- a/consul/discovery.go +++ b/consul/discovery.go @@ -23,6 +23,9 @@ type Service struct { agent *consul.Agent connect *connect.Service kv *consul.KV + + hcTicker *time.Ticker + ttlTicker *time.Ticker } var ErrServiceUnavailable = fmt.Errorf("Service is unavailable") @@ -104,25 +107,27 @@ func (s *Service) Unregister() error { ServiceID: s.GetID(), }, nil) + s.ttlTicker.Stop() + return s.agent.ServiceDeregister(s.GetID()) } func (s *Service) RegisterHealthChecks() { go func() { // startup register - ticker := time.NewTicker(time.Second) - for range ticker.C { + s.hcTicker = time.NewTicker(time.Second) + for range s.hcTicker.C { if ok, _ := s.healthCheck(); ok { - ticker.Stop() + s.hcTicker.Stop() } } }() go func() { // TTL - ticker := time.NewTicker(s.ttl) - for range ticker.C { + s.ttlTicker = time.NewTicker(s.ttl) + for range s.ttlTicker.C { if _, err := s.healthCheck(); err != nil { fmt.Printf("HealthCheck endpoint not available (%s)#: %v\n", s.GetFullAddr(), err) - ticker.Stop() + // s.ttlTicker.Stop() } } }()