From 0aa79c7d3571d8e08a4e2438645da7276a386161 Mon Sep 17 00:00:00 2001 From: Piotr Biernat Date: Wed, 28 Jun 2023 23:07:10 +0200 Subject: [PATCH] Consul debug and clean --- consul/discovery.go | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/consul/discovery.go b/consul/discovery.go index 8130dde..db5029c 100644 --- a/consul/discovery.go +++ b/consul/discovery.go @@ -33,7 +33,7 @@ func NewService(servAddr, id, name, hostname, domain, pathPrefix string, appPort s.domain = domain s.pathPrefix = pathPrefix s.port = appPort - s.ttl = time.Second * 15 + s.ttl = time.Second * 10 client, err := consul.NewClient(newClientConfig(servAddr)) if err != nil { @@ -70,7 +70,7 @@ func (s *Service) Register() error { Tags: s.getTags(), Check: &consul.AgentServiceCheck{ TTL: s.ttl.String(), - DeregisterCriticalServiceAfter: "5s", + DeregisterCriticalServiceAfter: "10s", }, } @@ -79,7 +79,7 @@ func (s *Service) Register() error { } go func(s *Service) { // startup register - ticker := time.NewTicker(time.Millisecond * 100) + ticker := time.NewTicker(time.Second * 1) for range ticker.C { if ok, _ := s.healthCheck(); ok { ticker.Stop() @@ -87,15 +87,15 @@ func (s *Service) Register() error { } }(s) - go func(s *Service) { // TTL - interval := s.ttl - time.Second*2 // 2 seconds overhead - ticker := time.NewTicker(interval) - for range ticker.C { - if _, err := s.healthCheck(); err != nil { - fmt.Printf("TTL Error #: %v\n", err) - } - } - }(s) + // go func(s *Service) { // TTL + // interval := s.ttl - time.Second*2 // 2 seconds overhead + // ticker := time.NewTicker(interval) + // for range ticker.C { + // if _, err := s.healthCheck(); err != nil { + // fmt.Printf("TTL Error #: %v\n", err) + // } + // } + // }(s) return nil } @@ -115,17 +115,14 @@ func (s *Service) healthCheck() (bool, error) { alive := func() bool { client := &http.Client{} healthUrl := s.GetFullAddr() + "health" - fmt.Printf("Sending request to the %s\n", healthUrl) req, err := http.NewRequest(http.MethodGet, healthUrl, nil) if err != nil { - fmt.Printf("Failed to create new Consul request: %v\n", err) return false } req.Header.Set("User-Agent", "Health Check") resp, err := client.Do(req) if err != nil { - fmt.Printf("Failed to send Consul request: %v\n", err) return false } defer resp.Body.Close()