2016-09-25 08:39:20 +01:00
|
|
|
// Package health implements an HTTP handler that responds to health checks.
|
2016-04-06 09:21:46 +01:00
|
|
|
package health
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
"log"
|
2016-04-29 07:28:35 +01:00
|
|
|
"net"
|
2016-04-06 09:21:46 +01:00
|
|
|
"net/http"
|
|
|
|
|
"sync"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var once sync.Once
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
type health struct {
|
2016-04-06 09:21:46 +01:00
|
|
|
Addr string
|
2016-08-19 17:14:17 -07:00
|
|
|
|
|
|
|
|
ln net.Listener
|
|
|
|
|
mux *http.ServeMux
|
2017-08-27 21:33:38 +01:00
|
|
|
|
2017-09-14 09:36:06 +01:00
|
|
|
// A slice of Healthers that the health plugin will poll every second for their health status.
|
2017-08-27 21:33:38 +01:00
|
|
|
h []Healther
|
|
|
|
|
sync.RWMutex
|
2017-09-14 09:36:06 +01:00
|
|
|
ok bool // ok is the global boolean indicating an all healthy plugin stack
|
2018-01-10 11:41:22 +00:00
|
|
|
|
|
|
|
|
stop chan bool
|
2016-04-06 09:21:46 +01:00
|
|
|
}
|
|
|
|
|
|
2018-01-10 11:41:22 +00:00
|
|
|
func (h *health) OnStartup() error {
|
2016-04-06 09:21:46 +01:00
|
|
|
if h.Addr == "" {
|
|
|
|
|
h.Addr = defAddr
|
|
|
|
|
}
|
2016-04-29 07:28:35 +01:00
|
|
|
|
2016-04-06 09:21:46 +01:00
|
|
|
once.Do(func() {
|
2016-09-23 09:14:12 +01:00
|
|
|
ln, err := net.Listen("tcp", h.Addr)
|
|
|
|
|
if err != nil {
|
2016-04-29 07:28:35 +01:00
|
|
|
log.Printf("[ERROR] Failed to start health handler: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2016-09-23 09:14:12 +01:00
|
|
|
|
|
|
|
|
h.ln = ln
|
|
|
|
|
|
2016-04-29 07:28:35 +01:00
|
|
|
h.mux = http.NewServeMux()
|
|
|
|
|
|
2016-09-23 09:14:12 +01:00
|
|
|
h.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
2017-08-27 21:33:38 +01:00
|
|
|
if h.Ok() {
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
io.WriteString(w, ok)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
2016-09-23 09:14:12 +01:00
|
|
|
})
|
|
|
|
|
|
2016-04-06 09:21:46 +01:00
|
|
|
go func() {
|
2016-04-29 07:28:35 +01:00
|
|
|
http.Serve(h.ln, h.mux)
|
2016-04-06 09:21:46 +01:00
|
|
|
}()
|
2018-01-10 11:41:22 +00:00
|
|
|
go func() {
|
|
|
|
|
h.overloaded()
|
|
|
|
|
}()
|
2016-04-06 09:21:46 +01:00
|
|
|
})
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 11:41:22 +00:00
|
|
|
func (h *health) OnShutdown() error {
|
2016-04-29 07:28:35 +01:00
|
|
|
if h.ln != nil {
|
|
|
|
|
return h.ln.Close()
|
|
|
|
|
}
|
2018-01-10 11:41:22 +00:00
|
|
|
|
|
|
|
|
h.stop <- true
|
|
|
|
|
|
2016-04-29 07:28:35 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-06 09:21:46 +01:00
|
|
|
const (
|
|
|
|
|
ok = "OK"
|
|
|
|
|
defAddr = ":8080"
|
2016-04-29 07:28:35 +01:00
|
|
|
path = "/health"
|
2016-04-06 09:21:46 +01:00
|
|
|
)
|