mw/health: poll other middleware (#976)

This add the infrastructure to let other middleware report their health
status back to the health middleware. A health.Healther interface is
introduced and a middleware needs to implement that. A middleware
that supports healthchecks is statically configured.

Every second each supported middleware is queried and the global health
state is updated.

Actual tests have been disabled as no other middleware implements this
at the moment.
This commit is contained in:
Miek Gieben
2017-08-27 21:33:38 +01:00
committed by Yong Tang
parent 9c56805d38
commit 558f4bea41
7 changed files with 149 additions and 22 deletions

View File

@@ -1,16 +1,11 @@
package health
import (
"fmt"
"io/ioutil"
"net/http"
"testing"
)
// TODO(miek): enable again if middleware gets health check.
/*
func TestHealth(t *testing.T) {
// We use a random port instead of a fixed port like 8080 that may have been
// occupied by some other process.
h := health{Addr: ":0"}
h.h = append(h.h, &erratic.Erratic{})
if err := h.Startup(); err != nil {
t.Fatalf("Unable to startup the health server: %v", err)
}
@@ -19,11 +14,23 @@ func TestHealth(t *testing.T) {
// Reconstruct the http address based on the port allocated by operating system.
address := fmt.Sprintf("http://%s%s", h.ln.Addr().String(), path)
// Norhing set should be unhealthy
response, err := http.Get(address)
if err != nil {
t.Fatalf("Unable to query %s: %v", address, err)
}
defer response.Body.Close()
if response.StatusCode != 503 {
t.Errorf("Invalid status code: expecting '503', got '%d'", response.StatusCode)
}
response.Body.Close()
// Make healthy
h.Poll()
response, err = http.Get(address)
if err != nil {
t.Fatalf("Unable to query %s: %v", address, err)
}
if response.StatusCode != 200 {
t.Errorf("Invalid status code: expecting '200', got '%d'", response.StatusCode)
}
@@ -31,7 +38,10 @@ func TestHealth(t *testing.T) {
if err != nil {
t.Fatalf("Unable to get response body from %s: %v", address, err)
}
if string(content) != "OK" {
response.Body.Close()
if string(content) != ok {
t.Errorf("Invalid response body: expecting 'OK', got '%s'", string(content))
}
}
*/