fix in consul discovery

This commit is contained in:
Piotr Biernat 2024-07-19 16:39:11 +02:00
parent 37fa05402e
commit 767eb5688c

View File

@ -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()
}
}
}()