-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.go
More file actions
554 lines (451 loc) · 15.1 KB
/
Copy pathservices.go
File metadata and controls
554 lines (451 loc) · 15.1 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
package controls
import (
"context"
"sync"
"time"
"gitlab.com/phpboyscout/go/errors"
)
const (
defaultInitialBackoff = 1 * time.Second
defaultMaxBackoff = 30 * time.Second
defaultHealthInterval = 10 * time.Second
backoffMultiplier = 2.0
// DefaultRestartResetInterval is the duration a service must run healthily
// before its consecutive-failure restart counter resets to zero.
DefaultRestartResetInterval = 30 * time.Second
)
// runOutcome classifies the result of a single service run for the supervisor.
type runOutcome int
const (
// outcomeCleanStart means Start returned nil — the service either completed
// cleanly or, more commonly, serves in a background goroutine. It is NOT an
// exit and is never restarted; such services are supervised via health checks.
outcomeCleanStart runOutcome = iota
// outcomeCancelled means the run ended because the controller context was
// cancelled (graceful shutdown). It never triggers a restart.
outcomeCancelled
// outcomeError means Start returned a non-nil, non-valid error while the
// context was still live. Only this outcome may trigger a restart.
outcomeError
)
// noopStop is the default StopFunc used when a service registers none.
func noopStop(context.Context) {}
// noopStart is the default StartFunc used when a service registers none.
func noopStart(context.Context) error { return nil }
// Services manages the collection of registered services and their lifecycle.
type Services struct {
mu sync.Mutex
services []Service
info sync.Map // map[string]ServiceInfo
validError ValidErrorFunc
// exits pairs each launched supervisor goroutine with the channel closed
// when it returns, so the shutdown sequence can bound its wait for
// supervisor exit and name any service whose StartFunc never returned (D10).
exits []supervisorExit
}
// supervisorExit records the exit channel for a single supervisor goroutine.
// The channel is closed when the goroutine returns.
type supervisorExit struct {
name string
exited chan struct{}
}
func (q *Services) add(s Service) {
q.mu.Lock()
defer q.mu.Unlock()
// D5: default missing lifecycle funcs to no-ops so the supervisor and the
// shutdown sequence never dereference a nil func.
if s.Start == nil {
s.Start = noopStart
}
if s.Stop == nil {
s.Stop = noopStop
}
q.services = append(q.services, s)
q.info.Store(s.Name, ServiceInfo{Name: s.Name})
}
// classifyRun determines the outcome of a single Start invocation. The validError
// predicate (if set) exempts expected terminal errors (e.g. http.ErrServerClosed)
// from being treated as failures.
func (q *Services) classifyRun(ctx context.Context, err error) runOutcome {
if err == nil {
return outcomeCleanStart
}
if ctx.Err() != nil || errors.Is(err, context.Canceled) {
return outcomeCancelled
}
if q.validError != nil && q.validError(err) {
return outcomeCancelled
}
return outcomeError
}
// monitorHealth supervises a background-serving service via its Status probe. It
// returns true if it ended because the health-failure threshold was breached (a
// restart-worthy condition), or false if it ended because the context was
// cancelled or there is nothing to monitor (in which case the service is supervised
// purely by its clean start and should simply wait for shutdown).
func (q *Services) monitorHealth(ctx context.Context, srv Service, updateInfo func(func(*ServiceInfo))) bool {
if srv.RestartPolicy.HealthFailureThreshold <= 0 || srv.Status == nil {
// Nothing to monitor: a clean start is not an exit, so block until the
// controller shuts down rather than falling through to a restart.
<-ctx.Done()
return false
}
healthInterval := srv.RestartPolicy.HealthCheckInterval
if healthInterval == 0 {
healthInterval = defaultHealthInterval
}
healthFailures := 0
for {
select {
case <-time.After(healthInterval):
if err := srv.Status(); err != nil {
healthFailures++
if healthFailures >= srv.RestartPolicy.HealthFailureThreshold {
srv.Stop(ctx)
updateInfo(func(i *ServiceInfo) {
i.Error = errors.Wrap(err, "health check failed")
})
return true
}
} else {
healthFailures = 0 // Reset on success
}
case <-ctx.Done():
return false
}
}
}
// sendErr forwards err on errs unless shutdown has already completed (D9). Once
// handleStopMessage closes done, the error/context handler has exited and there
// is no receiver, so an unguarded send would block the supervisor goroutine
// forever. The guard makes every forward provably non-blocking.
func sendErr(done <-chan struct{}, errs chan error, err error) {
select {
case errs <- err:
case <-done:
}
}
func (q *Services) start(ctx context.Context, wg *sync.WaitGroup, errChan chan error, done <-chan struct{}) {
q.mu.Lock()
defer q.mu.Unlock()
for _, s := range q.services {
exited := make(chan struct{})
q.exits = append(q.exits, supervisorExit{name: s.Name, exited: exited})
go func(s Service, exited chan struct{}) {
defer close(exited)
q.supervise(ctx, s, errChan, wg, done)
}(s, exited)
}
}
// awaitSupervisors blocks until every supervisor goroutine has exited or ctx is
// done, whichever comes first. It returns the names of services whose
// supervisors had not exited by the deadline — i.e. whose StartFuncs ignored
// cancellation. The stuck goroutines are abandoned rather than force-drained:
// decrementing the wait group on their behalf would race a late-returning
// Start into a double-decrement panic (D10).
func (q *Services) awaitSupervisors(ctx context.Context) []string {
q.mu.Lock()
exits := make([]supervisorExit, len(q.exits))
copy(exits, q.exits)
q.mu.Unlock()
var stuck []string
for _, e := range exits {
select {
case <-e.exited:
case <-ctx.Done():
// Deadline reached (or already elapsed when we got here). A final
// non-blocking check distinguishes "exited at the same instant"
// from genuinely stuck, so a service is never reported abandoned
// after its supervisor actually returned.
select {
case <-e.exited:
default:
stuck = append(stuck, e.name)
}
}
}
return stuck
}
func (q *Services) supervise(ctx context.Context, srv Service, errs chan error, wg *sync.WaitGroup, done <-chan struct{}) {
started := false
markStarted := func() {
if !started {
wg.Done()
started = true
}
}
defer markStarted() // ensure wg is decremented if we exit early
updateInfo := func(update func(*ServiceInfo)) {
if v, ok := q.info.Load(srv.Name); ok {
info := v.(ServiceInfo)
update(&info)
q.info.Store(srv.Name, info)
}
}
if srv.RestartPolicy == nil {
q.runOnce(ctx, srv, errs, updateInfo, done)
return
}
q.runWithRestartPolicy(ctx, srv, errs, markStarted, updateInfo, done)
}
func (q *Services) runOnce(ctx context.Context, srv Service, errs chan error, updateInfo func(func(*ServiceInfo)), done <-chan struct{}) {
updateInfo(func(i *ServiceInfo) { i.LastStarted = time.Now() })
err := srv.Start(ctx)
updateInfo(func(i *ServiceInfo) {
i.LastStopped = time.Now()
i.Error = err
})
// Only forward genuine errors; a clean start, a cancellation, or a valid
// terminal error (e.g. http.ErrServerClosed) is not a failure.
if q.classifyRun(ctx, err) == outcomeError {
sendErr(done, errs, err)
}
}
func calculateNextBackoff(current, max time.Duration) time.Duration {
next := time.Duration(float64(current) * backoffMultiplier)
if next > max || next < 0 {
return max
}
return next
}
// restartTimings holds the resolved backoff/reset parameters for a restart loop.
type restartTimings struct {
backoff time.Duration
maxBackoff time.Duration
resetInterval time.Duration
}
// initialBackoff resolves the starting backoff for a policy, applying the default
// when unset. Shared by the initial timings and the post-healthy-run reset.
func initialBackoff(p *RestartPolicy) time.Duration {
if p.InitialBackoff == 0 {
return defaultInitialBackoff
}
return p.InitialBackoff
}
func resolveRestartTimings(p *RestartPolicy) restartTimings {
t := restartTimings{
backoff: initialBackoff(p),
maxBackoff: p.MaxBackoff,
resetInterval: p.RestartResetInterval,
}
if t.maxBackoff == 0 {
t.maxBackoff = defaultMaxBackoff
}
if t.resetInterval == 0 {
t.resetInterval = DefaultRestartResetInterval
}
return t
}
// runOnceWithRestart performs a single supervised run. It returns the Start error
// and whether the restart loop should keep going (false means terminate: graceful
// shutdown or a clean start with no exit).
func (q *Services) runOnceWithRestart(ctx context.Context, srv Service, markStarted func(), updateInfo func(func(*ServiceInfo))) (error, bool) {
updateInfo(func(i *ServiceInfo) { i.LastStarted = time.Now() })
err := srv.Start(ctx)
updateInfo(func(i *ServiceInfo) {
i.LastStopped = time.Now()
i.Error = err
})
switch q.classifyRun(ctx, err) {
case outcomeCancelled:
// Graceful shutdown (or an expected terminal error). Never restart.
return err, false
case outcomeCleanStart:
// Start returned nil: the service serves in the background. Mark it
// started and supervise it via its health check. monitorHealth blocks
// until the context is cancelled (shutdown) or the health threshold is
// breached. A clean start that is not health-failed is never an exit.
markStarted()
return err, q.monitorHealth(ctx, srv, updateInfo)
default: // outcomeError
return err, true
}
}
func (q *Services) runWithRestartPolicy(ctx context.Context, srv Service, errs chan error, markStarted func(), updateInfo func(func(*ServiceInfo)), done <-chan struct{}) {
restarts := 0
timings := resolveRestartTimings(srv.RestartPolicy)
for {
runStarted := time.Now()
err, keepGoing := q.runOnceWithRestart(ctx, srv, markStarted, updateInfo)
if !keepGoing {
return
}
// The run ended in a failure (Start error or health breach). If it ran
// healthily for long enough, reset both the consecutive-failure counter
// and the backoff (F6a) — otherwise a service healthy for hours still
// waits the accumulated MaxBackoff before its next restart.
if time.Since(runStarted) >= timings.resetInterval {
restarts = 0
timings.backoff = initialBackoff(srv.RestartPolicy)
}
// Check if we've exhausted restarts.
if srv.RestartPolicy.MaxRestarts > 0 && restarts >= srv.RestartPolicy.MaxRestarts {
finalErr := errors.New("max restarts exceeded")
if err != nil {
finalErr = errors.Wrap(err, "max restarts exceeded")
}
updateInfo(func(i *ServiceInfo) { i.Error = finalErr })
sendErr(done, errs, finalErr)
return
}
restarts++
updateInfo(func(i *ServiceInfo) { i.RestartCount = restarts })
// Never send nil on errs (errors.Wrap(nil,...) returns nil). A health
// failure stores its error via monitorHealth/updateInfo; only forward a
// non-nil Start error here.
if err != nil {
sendErr(done, errs, err)
}
// Wait for backoff or cancellation.
select {
case <-time.After(timings.backoff):
timings.backoff = calculateNextBackoff(timings.backoff, timings.maxBackoff)
continue
case <-ctx.Done():
return
}
}
}
// stop shuts services down in reverse registration order, one at a time. Each
// StopFunc runs in its own goroutine and is awaited against ctx.Done(): a
// context-ignoring stop is abandoned at the shutdown deadline rather than hanging
// the caller (and Wait()) forever. The abandoned goroutine is left to finish on
// its own. Returns the number of services.
//
// The service slice is snapshotted under the lock and the lock is then released
// for the whole stop sequence (D12). Holding q.mu while awaiting every StopFunc —
// up to the entire shutdown timeout — would block status()/liveness()/readiness()
// on the same mutex, so every health probe would hang exactly when a load
// balancer most needs a prompt not-ready answer. Registration is impossible once
// the controller is Stopping, so the snapshot cannot go stale.
func (q *Services) stop(ctx context.Context) int {
q.mu.Lock()
services := make([]Service, len(q.services))
copy(services, q.services)
q.mu.Unlock()
for i := len(services) - 1; i >= 0; i-- {
s := services[i]
done := make(chan struct{})
go func() {
defer close(done)
callStop(ctx, s.Stop)
}()
select {
case <-done:
// Stop completed within the remaining deadline.
case <-ctx.Done():
// Deadline reached: abandon this stop and move on to the next service.
// Remaining stops still get a best-effort attempt, but with the
// deadline already elapsed their own ctx.Done() fires immediately.
// The abandoned goroutine is left to finish on its own.
}
}
return len(services)
}
// callStop invokes a StopFunc, recovering from a panic so a misbehaving stop
// cannot crash the shutdown sequence. fn is never nil (defaulted at registration).
func callStop(ctx context.Context, fn StopFunc) {
defer func() {
_ = recover()
}()
fn(ctx)
}
// callProbe calls fn and returns any error it produces. If fn panics, the panic
// value is converted to an error so that a misbehaving StatusFunc or ProbeFunc
// cannot crash the server.
func callProbe(fn func() error) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.Newf("probe panicked: %v", r)
}
}()
return fn()
}
func (q *Services) status() HealthReport {
q.mu.Lock()
defer q.mu.Unlock()
report := HealthReport{
OverallHealthy: true,
Services: make([]ServiceStatus, 0, len(q.services)),
}
for _, s := range q.services {
status := ServiceStatus{
Name: s.Name,
Status: "OK",
}
if s.Status != nil {
if err := callProbe(s.Status); err != nil {
status.Status = "ERROR"
status.Error = err.Error()
report.OverallHealthy = false
}
}
report.Services = append(report.Services, status)
}
return report
}
func (q *Services) liveness() HealthReport {
q.mu.Lock()
defer q.mu.Unlock()
report := HealthReport{
OverallHealthy: true,
Services: make([]ServiceStatus, 0, len(q.services)),
}
for _, s := range q.services {
status := ServiceStatus{
Name: s.Name,
Status: "OK",
}
var err error
if s.Liveness != nil {
err = callProbe(s.Liveness)
} else if s.Status != nil {
err = callProbe(s.Status)
}
if err != nil {
status.Status = "ERROR"
status.Error = err.Error()
report.OverallHealthy = false
}
report.Services = append(report.Services, status)
}
return report
}
func (q *Services) readiness() HealthReport {
q.mu.Lock()
defer q.mu.Unlock()
report := HealthReport{
OverallHealthy: true,
Services: make([]ServiceStatus, 0, len(q.services)),
}
for _, s := range q.services {
status := ServiceStatus{
Name: s.Name,
Status: "OK",
}
var err error
if s.Readiness != nil {
err = callProbe(s.Readiness)
} else if s.Status != nil {
err = callProbe(s.Status)
}
if err != nil {
status.Status = "ERROR"
status.Error = err.Error()
report.OverallHealthy = false
}
report.Services = append(report.Services, status)
}
return report
}
// Service represents a managed background service with start/stop lifecycle,
// health probes, and optional restart policy.
type Service struct {
Name string
Start StartFunc
Stop StopFunc
Status StatusFunc
Liveness ProbeFunc
Readiness ProbeFunc
RestartPolicy *RestartPolicy
}