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

@@ -16,6 +16,11 @@ type health struct {
ln net.Listener
mux *http.ServeMux
// A slice of Healthers that the health middleware will poll every second for their health status.
h []Healther
sync.RWMutex
ok bool // ok is the global boolean indicating an all healthy middleware stack
}
func (h *health) Startup() error {
@@ -35,7 +40,12 @@ func (h *health) Startup() error {
h.mux = http.NewServeMux()
h.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, ok)
if h.Ok() {
w.WriteHeader(http.StatusOK)
io.WriteString(w, ok)
return
}
w.WriteHeader(http.StatusServiceUnavailable)
})
go func() {