-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhealth_checks.go
44 lines (35 loc) · 969 Bytes
/
health_checks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package run
import (
"net/http"
)
// A Probe responds to health checks.
type Probe interface {
Ready() bool
}
type httpProbeHandler struct {
probe Probe
}
// Ready replies to the request with an HTTP 200 response.
func Ready(w http.ResponseWriter, r *http.Request) {
Info(r, "HTTP startup probe succeeded")
w.WriteHeader(200)
}
// Healthy replies to the request with an HTTP 200 response.
func Healthy(w http.ResponseWriter, r *http.Request) {
Info(r, "HTTP liveness probe succeeded")
w.WriteHeader(200)
}
// HTTPProbeHandler returns a request handler that calls the given probe and
// returns an HTTP 200 response if the probe Ready method returns true, or an
// HTTP 500 if false.
func HTTPProbeHandler(probe Probe) http.Handler {
return &httpProbeHandler{probe: probe}
}
func (h *httpProbeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if h.probe.Ready() {
w.WriteHeader(200)
return
}
w.WriteHeader(500)
}