-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprobe.go
More file actions
221 lines (183 loc) · 4.42 KB
/
probe.go
File metadata and controls
221 lines (183 loc) · 4.42 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package initd
import (
"context"
"sync"
"time"
)
// note: it's set to 9 seconds, to be ahead of the default k8s 10s check period.
const defaultProbeInterval = 9 * time.Second
type probeKind int
const (
readinessProbe probeKind = iota
livenessProbe
startupProbe
)
// ProbeOption configures probe behavior.
type ProbeOption func(*probeConfig)
type probeConfig struct {
timeout time.Duration
interval time.Duration
failAfter int
initialDelay time.Duration
oneShot bool
}
// ProbeTimeout sets the per-evaluation context timeout.
func ProbeTimeout(d time.Duration) ProbeOption {
return func(c *probeConfig) { c.timeout = d }
}
// ProbeInterval sets how often the probe is evaluated in the background.
// Default is 9s.
func ProbeInterval(d time.Duration) ProbeOption {
return func(c *probeConfig) { c.interval = d }
}
// ProbeFailAfter marks the probe unhealthy only after n consecutive failures.
func ProbeFailAfter(n int) ProbeOption {
return func(c *probeConfig) { c.failAfter = n }
}
// ProbeInitialDelay waits before starting periodic evaluation.
func ProbeInitialDelay(d time.Duration) ProbeOption {
return func(c *probeConfig) { c.initialDelay = d }
}
func probeOneShot() ProbeOption {
return func(c *probeConfig) { c.oneShot = true }
}
// ProbeResult is the aggregate result of all probes of a kind.
type ProbeResult struct {
Healthy bool
Checks map[string]CheckResult
}
// CheckResult is the last-known result of a single probe.
type CheckResult struct {
Healthy bool
Duration time.Duration
Error string
}
type probe struct {
name string
fn func(context.Context) error
config probeConfig
mu sync.RWMutex
lastResult CheckResult
consecutiveFails int
}
func newProbe(name string, fn func(context.Context) error, opts ...ProbeOption) *probe {
var cfg probeConfig
for _, o := range opts {
o(&cfg)
}
if cfg.interval == 0 {
cfg.interval = defaultProbeInterval
}
return &probe{
name: name,
fn: fn,
config: cfg,
lastResult: CheckResult{Healthy: false},
}
}
func (p *probe) run(ctx context.Context) {
if p.config.initialDelay > 0 {
select {
case <-time.After(p.config.initialDelay):
case <-ctx.Done():
return
}
}
if p.evaluate(ctx) && p.config.oneShot {
return
}
ticker := time.NewTicker(p.config.interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
p.evaluate(ctx)
case <-ctx.Done():
p.evaluateDone()
return
}
}
}
func (p *probe) evaluate(ctx context.Context) (healthy bool) {
if p.config.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, p.config.timeout)
defer cancel()
}
start := time.Now()
err := p.fn(ctx)
d := time.Since(start)
p.mu.Lock()
defer p.mu.Unlock()
if err != nil {
p.consecutiveFails++
result := CheckResult{Duration: d, Error: err.Error()}
if p.config.failAfter > 0 && p.consecutiveFails < p.config.failAfter {
result.Healthy = p.lastResult.Healthy
}
p.lastResult = result
return p.lastResult.Healthy
}
p.consecutiveFails = 0
p.lastResult = CheckResult{Healthy: true, Duration: d}
return p.lastResult.Healthy
}
func (p *probe) evaluateDone() {
p.mu.Lock()
defer p.mu.Unlock()
p.lastResult = CheckResult{Healthy: false}
}
func (p *probe) result() CheckResult {
p.mu.RLock()
defer p.mu.RUnlock()
return p.lastResult
}
type probeRegistry struct {
mu sync.RWMutex
ctx context.Context
readiness []*probe
liveness []*probe
startup []*probe
}
func newProbeRegistry(ctx context.Context) *probeRegistry {
return &probeRegistry{ctx: ctx}
}
func (r *probeRegistry) register(kind probeKind, name string, fn func(context.Context) error, opts ...ProbeOption) {
p := newProbe(name, fn, opts...)
r.mu.Lock()
switch kind {
case readinessProbe:
r.readiness = append(r.readiness, p)
case livenessProbe:
r.liveness = append(r.liveness, p)
case startupProbe:
r.startup = append(r.startup, p)
}
r.mu.Unlock()
go p.run(r.ctx)
}
func (r *probeRegistry) check(kind probeKind) ProbeResult {
r.mu.RLock()
var probes []*probe
switch kind {
case readinessProbe:
probes = r.readiness
case livenessProbe:
probes = r.liveness
case startupProbe:
probes = r.startup
}
r.mu.RUnlock()
result := ProbeResult{
Healthy: true,
Checks: make(map[string]CheckResult, len(probes)),
}
for _, p := range probes {
cr := p.result()
result.Checks[p.name] = cr
if !cr.Healthy {
result.Healthy = false
}
}
return result
}