fix ticker, variable names and unnecessary if in Service.Match

This commit is contained in:
nikolay.bystritskiy
2021-05-16 21:09:20 +02:00
committed by Umputun
parent e02ddaf256
commit bfe5f3fdbf
2 changed files with 7 additions and 10 deletions

View File

@@ -138,10 +138,7 @@ func (s *Service) Match(srv, src string) (string, MatchType, bool) {
case MTProxy:
dest := m.SrcMatch.ReplaceAllString(src, m.Dst)
if src != dest {
if m.IsAlive() {
return dest, m.MatchType, true
}
return dest, m.MatchType, false
return dest, m.MatchType, m.IsAlive()
}
case MTStatic:
if src == m.AssetsWebRoot || strings.HasPrefix(src, m.AssetsWebRoot+"/") {
@@ -160,15 +157,16 @@ func (s *Service) ScheduleHealthCheck(ctx context.Context, interval time.Duratio
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
pinged := s.CheckHealth()
pingErrs := s.CheckHealth()
s.lock.Lock()
for _, mappers := range s.mappers {
for i := range mappers {
if err, ok := pinged[mappers[i].PingURL]; ok {
if err, ok := pingErrs[mappers[i].PingURL]; ok {
mappers[i].dead = false
if err != nil {
mappers[i].dead = true
@@ -178,7 +176,6 @@ func (s *Service) ScheduleHealthCheck(ctx context.Context, interval time.Duratio
}
s.lock.Unlock()
case <-ctx.Done():
ticker.Stop()
return
}
}

View File

@@ -32,10 +32,10 @@ func (h *Http) healthHandler(w http.ResponseWriter, _ *http.Request) {
}
}
pingRes := h.CheckHealth()
pingErrs := h.CheckHealth()
var errs []string
for _, pingErr := range pingRes {
for _, pingErr := range pingErrs {
if pingErr != nil {
errs = append(errs, pingErr.Error())
}
@@ -50,7 +50,7 @@ func (h *Http) healthHandler(w http.ResponseWriter, _ *http.Request) {
Passed int `json:"passed,omitempty"`
Failed int `json:"failed,omitempty"`
Errors []string `json:"errors,omitempty"`
}{Status: "failed", Services: total, Passed: len(pingRes) - len(errs), Failed: len(errs), Errors: errs}
}{Status: "failed", Services: total, Passed: len(pingErrs) - len(errs), Failed: len(errs), Errors: errs}
rest.RenderJSON(w, errResp)
return