mirror of
https://github.com/umputun/reproxy.git
synced 2024-10-26 10:10:12 +03:00
fix health counts calculation
This commit is contained in:
parent
1695532aa5
commit
f7b24e1058
@ -32,10 +32,14 @@ func (h *Http) healthHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
// runs pings in parallel
|
||||
check := func(mappers []discovery.URLMapper) (ok bool, valid int, total int, errs []string) {
|
||||
outCh := make(chan error, concurrent)
|
||||
pinged := 0
|
||||
services, pinged := 0, 0
|
||||
var wg sync.WaitGroup
|
||||
for _, m := range mappers {
|
||||
if m.MatchType != discovery.MTProxy || m.PingURL == "" {
|
||||
if m.MatchType != discovery.MTProxy {
|
||||
continue
|
||||
}
|
||||
services++
|
||||
if m.PingURL == "" {
|
||||
continue
|
||||
}
|
||||
sema <- struct{}{}
|
||||
@ -73,7 +77,7 @@ func (h *Http) healthHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
for e := range outCh {
|
||||
errs = append(errs, e.Error())
|
||||
}
|
||||
return len(errs) == 0, pinged - len(errs), len(mappers), errs
|
||||
return len(errs) == 0, pinged - len(errs), services, errs
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
|
||||
@ -87,7 +91,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: valid, Failed: total - valid, Errors: errs}
|
||||
}{Status: "failed", Services: total, Passed: valid, Failed: len(errs), Errors: errs}
|
||||
|
||||
rest.RenderJSON(w, errResp)
|
||||
return
|
||||
|
@ -44,6 +44,8 @@ func TestHttp_healthHandler(t *testing.T) {
|
||||
"localhost,^/api/(.*)," + ds.URL + "/123/$1," + ps.URL + "/123/ping",
|
||||
"localhost,^/xyz/(.*)," + ds.URL + "/123/$1," + ps.URL + "/xxx/ping",
|
||||
"127.0.0.1,^/api/(.*)," + ds.URL + "/567/$1," + ps.URL + "/567/ping",
|
||||
"127.0.0.1,^/api/(.*)," + ds.URL + "/567/$1,",
|
||||
"127.0.0.1,^/api/(.*),assets:/567/$1," + ps.URL + "/567/ping",
|
||||
},
|
||||
}}, time.Millisecond*10)
|
||||
|
||||
@ -69,7 +71,7 @@ func TestHttp_healthHandler(t *testing.T) {
|
||||
err = json.NewDecoder(resp.Body).Decode(&res)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "failed", res["status"])
|
||||
assert.Equal(t, 3., res["services"])
|
||||
assert.Equal(t, 4., res["services"])
|
||||
assert.Equal(t, 1., res["passed"])
|
||||
assert.Equal(t, 2., res["failed"])
|
||||
assert.Equal(t, 2, len(res["errors"].([]interface{})))
|
||||
|
Loading…
Reference in New Issue
Block a user