-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.go
144 lines (127 loc) · 3.99 KB
/
status.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
package httpstatus
import (
"context"
"errors"
"io"
"net/http"
"sync/atomic"
"time"
)
type defaultStatus struct {
monitor
resourceName string
state uint32
stateHandlers [4]http.Handler
hardContext context.Context
softContext context.Context
shutdown context.CancelFunc
healthCheck HealthCheck
timeout time.Duration
frequency time.Duration
delay time.Duration
logger logger
}
func newHandler(config configuration) Handler {
softContext, softShutdown := context.WithCancel(config.ctx)
var stateHandlers [4]http.Handler
stateHandlers[stateStarting] = newStateHandler(http.StatusServiceUnavailable, config.displayName, config.resourceName, config.startingState, config.version)
stateHandlers[stateStopping] = newStateHandler(http.StatusServiceUnavailable, config.displayName, config.resourceName, config.stoppingState, config.version)
stateHandlers[stateFailing] = newStateHandler(http.StatusServiceUnavailable, config.displayName, config.resourceName, config.failingState, config.version)
stateHandlers[stateHealthy] = newStateHandler(http.StatusOK, config.displayName, config.resourceName, config.healthyState, config.version)
return &defaultStatus{
monitor: config.monitor,
resourceName: config.resourceName,
stateHandlers: stateHandlers,
hardContext: config.ctx,
softContext: softContext,
shutdown: softShutdown,
healthCheck: config.healthCheck,
timeout: config.healthCheckTimeout,
frequency: config.healthCheckFrequency,
delay: config.shutdownDelay,
logger: config.logger,
}
}
func (this *defaultStatus) ServeHTTP(response http.ResponseWriter, request *http.Request) {
state := atomic.LoadUint32(&this.state)
handler := this.stateHandlers[state]
handler.ServeHTTP(response, request)
}
func (this *defaultStatus) Listen() {
defer this.Stopping()
for this.isAlive() && this.checkHealth() {
}
}
func (this *defaultStatus) checkHealth() bool {
ctx, cancel := context.WithTimeout(this.softContext, this.timeout)
defer cancel()
if err := this.healthCheck.Status(ctx); err == nil {
this.Healthy()
} else if errors.Is(err, context.Canceled) {
return false
} else {
this.Failing(err)
}
this.awaitNextCheck()
return true
}
func (this *defaultStatus) awaitNextCheck() {
ctx, cancel := context.WithTimeout(this.softContext, this.frequency)
defer cancel()
<-ctx.Done()
}
func (this *defaultStatus) isAlive() bool {
select {
case <-this.softContext.Done():
return false
default:
return true
}
}
func (this *defaultStatus) Healthy() {
if atomic.SwapUint32(&this.state, stateHealthy) == stateHealthy {
return // state hasn't changed, previously healthy
}
this.monitor.Healthy()
this.logger.Printf("[INFO] Health check for resource [%s] passed.", this.resourceName)
}
func (this *defaultStatus) Failing(err error) {
if atomic.SwapUint32(&this.state, stateFailing) == stateFailing {
return // state hasn't changed, previously failing
}
this.monitor.Failing(err)
this.logger.Printf("[WARN] Health check for resource [%s] failing: [%s].", this.resourceName, err)
}
func (this *defaultStatus) Stopping() {
previousState := atomic.SwapUint32(&this.state, stateStopping)
this.monitor.Stopping()
if previousState != stateHealthy {
this.logger.Printf("[INFO] Health check for resource [%s] entering [stopping] state. Skipping [%s] shutdown delay because state is unhealthy.", this.resourceName, this.delay)
return // already unhealthy, avoid shutdown delay
}
this.logger.Printf("[INFO] Health check for resource [%s] entering [stopping] state. Waiting [%s] before concluding.", this.resourceName, this.delay)
ctx, cancel := context.WithTimeout(this.hardContext, this.delay)
defer cancel()
<-ctx.Done()
}
func (this *defaultStatus) Close() error {
tryClose(this.healthCheck)
this.shutdown()
return nil
}
func tryClose(v any) {
if v == nil {
return
}
closer, ok := v.(io.Closer)
if !ok {
return
}
_ = closer.Close()
}
const (
stateStarting = iota
stateHealthy
stateFailing
stateStopping
)