-
Notifications
You must be signed in to change notification settings - Fork 9
/
healthz.go
146 lines (120 loc) · 3.54 KB
/
healthz.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package core
import (
"context"
"errors"
"fmt"
"net/http"
"sync"
"time"
"go.uber.org/atomic"
)
var healthz struct {
mu sync.RWMutex
liveNames []string
liveChecker []Checker
readinessNames []string
readinessChecker []Checker
}
const defaultProbeTimeout = 1 * time.Second
type Checker interface {
Check(context.Context) error
}
// After the checker function returns a nil error, it will always succeed.
func CheckAtStartupFunc(callback func(context.Context) error) Checker {
return &memoizingChecker{checker: CheckerFunc(callback)}
}
type CheckerFunc func(context.Context) error
func (c CheckerFunc) Check(ctx context.Context) error { return c(ctx) }
var shutdownStarted = atomic.NewBool(false)
func init() {
registerReadiness("shutdown-requested", CheckerFunc(func(ctx context.Context) error {
if shutdownStarted.Load() {
return errors.New("shutdown requested")
}
return nil
}))
}
func MarkShutdownStarted() {
shutdownStarted.Store(true)
}
type memoizingChecker struct {
checker Checker
succeeded atomic.Bool
}
func (m *memoizingChecker) Check(ctx context.Context) error {
if m.succeeded.Load() {
return nil
}
err := m.checker.Check(ctx)
if err == nil {
m.succeeded.Store(true)
}
return err
}
func (m *memoizingChecker) isManual() bool { return false }
func registerLiveness(name string, checker Checker) {
healthz.mu.Lock()
healthz.liveNames = append(healthz.liveNames, name)
healthz.liveChecker = append(healthz.liveChecker, checker)
healthz.mu.Unlock()
}
func registerReadiness(name string, checker Checker) {
healthz.mu.Lock()
healthz.readinessNames = append(healthz.readinessNames, name)
healthz.readinessChecker = append(healthz.readinessChecker, checker)
healthz.mu.Unlock()
}
func livezEndpoint() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
healthz.mu.RLock()
checkers := make([]Checker, len(healthz.liveChecker))
copy(checkers, healthz.liveChecker)
names := make([]string, len(healthz.liveNames))
copy(names, healthz.liveNames)
healthz.mu.RUnlock()
// Run checks on a copy to guarantee we never block other /livez or /readyz calls.
runChecks(rw, r, names, checkers)
})
}
func readyzEndpoint() http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
healthz.mu.RLock()
checkers := make([]Checker, len(healthz.readinessChecker))
copy(checkers, healthz.readinessChecker)
names := make([]string, len(healthz.readinessNames))
copy(names, healthz.readinessNames)
healthz.mu.RUnlock()
// Run checks on a copy to guarantee we never block other /livez or /readyz calls.
runChecks(rw, r, names, checkers)
})
}
func runChecks(rw http.ResponseWriter, r *http.Request, names []string, checkers []Checker) {
ctx, done := context.WithTimeout(r.Context(), defaultProbeTimeout)
defer done()
errs := make([]error, len(checkers))
errCount := 0
for k, checker := range checkers {
// XXX guard against panic?
errs[k] = checker.Check(ctx)
if errs[k] != nil {
errCount++
}
}
if errCount > 0 {
rw.WriteHeader(500)
fmt.Fprintf(rw, "%d failures in %d checks\n\n", errCount, len(errs))
} else {
rw.WriteHeader(200)
fmt.Fprintf(rw, "All OK\n\n")
}
for k, name := range names {
if errs[k] == nil {
fmt.Fprintf(rw, "%s: OK\n", name)
} else {
fmt.Fprintf(rw, "%s: failed: %v\n", name, errs[k])
}
}
}