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.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()