Consul debug and clean

This commit is contained in:
Piotr Biernat 2023-06-28 23:07:10 +02:00
parent d484c0e3b0
commit 0aa79c7d35

View File

@ -33,7 +33,7 @@ func NewService(servAddr, id, name, hostname, domain, pathPrefix string, appPort
s.domain = domain s.domain = domain
s.pathPrefix = pathPrefix s.pathPrefix = pathPrefix
s.port = appPort s.port = appPort
s.ttl = time.Second * 15 s.ttl = time.Second * 10
client, err := consul.NewClient(newClientConfig(servAddr)) client, err := consul.NewClient(newClientConfig(servAddr))
if err != nil { if err != nil {
@ -70,7 +70,7 @@ func (s *Service) Register() error {
Tags: s.getTags(), Tags: s.getTags(),
Check: &consul.AgentServiceCheck{ Check: &consul.AgentServiceCheck{
TTL: s.ttl.String(), TTL: s.ttl.String(),
DeregisterCriticalServiceAfter: "5s", DeregisterCriticalServiceAfter: "10s",
}, },
} }
@ -79,7 +79,7 @@ func (s *Service) Register() error {
} }
go func(s *Service) { // startup register go func(s *Service) { // startup register
ticker := time.NewTicker(time.Millisecond * 100) ticker := time.NewTicker(time.Second * 1)
for range ticker.C { for range ticker.C {
if ok, _ := s.healthCheck(); ok { if ok, _ := s.healthCheck(); ok {
ticker.Stop() ticker.Stop()
@ -87,15 +87,15 @@ func (s *Service) Register() error {
} }
}(s) }(s)
go func(s *Service) { // TTL // go func(s *Service) { // TTL
interval := s.ttl - time.Second*2 // 2 seconds overhead // interval := s.ttl - time.Second*2 // 2 seconds overhead
ticker := time.NewTicker(interval) // ticker := time.NewTicker(interval)
for range ticker.C { // for range ticker.C {
if _, err := s.healthCheck(); err != nil { // if _, err := s.healthCheck(); err != nil {
fmt.Printf("TTL Error #: %v\n", err) // fmt.Printf("TTL Error #: %v\n", err)
} // }
} // }
}(s) // }(s)
return nil return nil
} }
@ -115,17 +115,14 @@ func (s *Service) healthCheck() (bool, error) {
alive := func() bool { alive := func() bool {
client := &http.Client{} client := &http.Client{}
healthUrl := s.GetFullAddr() + "health" healthUrl := s.GetFullAddr() + "health"
fmt.Printf("Sending request to the %s\n", healthUrl)
req, err := http.NewRequest(http.MethodGet, healthUrl, nil) req, err := http.NewRequest(http.MethodGet, healthUrl, nil)
if err != nil { if err != nil {
fmt.Printf("Failed to create new Consul request: %v\n", err)
return false return false
} }
req.Header.Set("User-Agent", "Health Check") req.Header.Set("User-Agent", "Health Check")
resp, err := client.Do(req) resp, err := client.Do(req)
if err != nil { if err != nil {
fmt.Printf("Failed to send Consul request: %v\n", err)
return false return false
} }
defer resp.Body.Close() defer resp.Body.Close()