-
Notifications
You must be signed in to change notification settings - Fork 0
/
probes.go
59 lines (45 loc) · 1.31 KB
/
probes.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package healthz
import (
"net/http"
)
// CheckFunc is a function that returns true if the probe is passing
type CheckFunc func() bool
func defaultCheckFunc() bool {
return true
}
var (
liveness CheckFunc = defaultCheckFunc
readiness CheckFunc = defaultCheckFunc
)
// LivenessCheck sets the probe for the /healthz/alive endpoint
func LivenessCheck(fn CheckFunc) {
if fn == nil {
return
}
liveness = fn
}
// ReadinessCheck sets the probe for the /healthz/ready endpoint
func ReadinessCheck(fn CheckFunc) {
if fn == nil {
return
}
readiness = fn
}
func checkHandler(fn CheckFunc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if fn() {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusServiceUnavailable)
}
})
}
// CreateMux creates an *http.ServeMux with handlers for /healthz/alive and /healthz/ready.
// The endpoints will return 200 OK if their respective CheckFunc returns true and 503 Service Unavailable if it returns false.
// If no CheckFunc was specified for the corresponding endpoint, the default probe is used and always returns true
func CreateMux() *http.ServeMux {
router := http.NewServeMux()
router.Handle("/healthz/alive", checkHandler(liveness))
router.Handle("/healthz/ready", checkHandler(readiness))
return router
}