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 agent *consul.Agent
connect *connect.Service connect *connect.Service
kv *consul.KV kv *consul.KV
hcTicker *time.Ticker
ttlTicker *time.Ticker
} }
var ErrServiceUnavailable = fmt.Errorf("Service is unavailable") var ErrServiceUnavailable = fmt.Errorf("Service is unavailable")
@ -104,25 +107,27 @@ func (s *Service) Unregister() error {
ServiceID: s.GetID(), ServiceID: s.GetID(),
}, nil) }, nil)
s.ttlTicker.Stop()
return s.agent.ServiceDeregister(s.GetID()) return s.agent.ServiceDeregister(s.GetID())
} }
func (s *Service) RegisterHealthChecks() { func (s *Service) RegisterHealthChecks() {
go func() { // startup register go func() { // startup register
ticker := time.NewTicker(time.Second) s.hcTicker = time.NewTicker(time.Second)
for range ticker.C { for range s.hcTicker.C {
if ok, _ := s.healthCheck(); ok { if ok, _ := s.healthCheck(); ok {
ticker.Stop() s.hcTicker.Stop()
} }
} }
}() }()
go func() { // TTL go func() { // TTL
ticker := time.NewTicker(s.ttl) s.ttlTicker = time.NewTicker(s.ttl)
for range ticker.C { for range s.ttlTicker.C {
if _, err := s.healthCheck(); err != nil { if _, err := s.healthCheck(); err != nil {
fmt.Printf("HealthCheck endpoint not available (%s)#: %v\n", s.GetFullAddr(), err) fmt.Printf("HealthCheck endpoint not available (%s)#: %v\n", s.GetFullAddr(), err)
ticker.Stop() // s.ttlTicker.Stop()
} }
} }
}() }()