-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
176 lines (153 loc) · 5.58 KB
/
config.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
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
package httpstatus
import (
"context"
"time"
)
type configuration struct {
displayName string
resourceName string
startingState string
healthyState string
failingState string
stoppingState string
version string
healthCheck HealthCheck
healthCheckFrequency time.Duration
healthCheckTimeout time.Duration
shutdownDelay time.Duration
ctx context.Context
monitor monitor
logger logger
}
func New(options ...option) Handler {
var config configuration
Options.apply(options...)(&config)
return newHandler(config)
}
var Options singleton
type singleton struct{}
type option func(*configuration)
func (singleton) DisplayName(value string) option {
return func(this *configuration) { this.displayName = value }
}
func (singleton) ResourceName(value string) option {
return func(this *configuration) { this.resourceName = value }
}
func (singleton) StartingStateValue(value string) option {
return func(this *configuration) { this.startingState = value }
}
func (singleton) HealthyStateValue(value string) option {
return func(this *configuration) { this.healthyState = value }
}
func (singleton) FailingStateValue(value string) option {
return func(this *configuration) { this.failingState = value }
}
func (singleton) StoppingStateValue(value string) option {
return func(this *configuration) { this.stoppingState = value }
}
func (singleton) HealthCheckFunc(value healthCheckFunc) option {
return Options.HealthCheck(NewSimpleHealthCheck(value))
}
func (singleton) SQLHealthCheck(value PingContext) option {
return Options.HealthCheck(NewPingHealthCheck(value))
}
func (singleton) HealthCheck(value HealthCheck) option {
return func(this *configuration) { this.healthCheck = value }
}
func (singleton) HealthChecks(all ...HealthCheck) option {
return Options.HealthCheck(NewCompositeHealthCheck(all...))
}
func (singleton) HealthCheckFrequency(value time.Duration) option {
return func(this *configuration) { this.healthCheckFrequency = value }
}
func (singleton) HealthCheckTimeout(value time.Duration) option {
return func(this *configuration) { this.healthCheckTimeout = value }
}
func (singleton) ShutdownDelay(value time.Duration) option {
return func(this *configuration) { this.shutdownDelay = value }
}
func (singleton) Context(value context.Context) option {
return func(this *configuration) { this.ctx = value }
}
func (singleton) Logger(value logger) option {
return func(this *configuration) { this.logger = value }
}
func (singleton) Monitor(value monitor) option {
return func(this *configuration) { this.monitor = value }
}
func (singleton) VersionName(value string) option {
return func(this *configuration) { this.version = value }
}
func (singleton) apply(options ...option) option {
return func(this *configuration) {
for _, item := range Options.defaults(options...) {
item(this)
}
}
}
func (singleton) defaults(options ...option) []option {
const defaultName = "status"
const defaultStartingState = "Starting"
const defaultHealthyState = "OK"
const defaultFailingState = "Failing"
const defaultStoppingState = "Stopping"
const defaultHealthCheckFrequency = time.Second
const defaultHealthCheckTimeout = time.Second * 10
const defaultShutdownDelay = 0
const defaultVersion = ""
var defaultContext = context.Background()
var defaultHealthCheck = nop{}
var defaultMonitor = nop{}
var defaultLogger = nop{}
return append([]option{
Options.DisplayName(defaultName),
Options.ResourceName(defaultName),
Options.StartingStateValue(defaultStartingState),
Options.HealthyStateValue(defaultHealthyState),
Options.FailingStateValue(defaultFailingState),
Options.StoppingStateValue(defaultStoppingState),
Options.HealthCheckFrequency(defaultHealthCheckFrequency),
Options.HealthCheckTimeout(defaultHealthCheckTimeout),
Options.ShutdownDelay(defaultShutdownDelay),
Options.VersionName(defaultVersion),
Options.Context(defaultContext),
Options.HealthCheckFunc(defaultHealthCheck.Status),
Options.HealthCheck(defaultHealthCheck),
Options.Monitor(defaultMonitor),
Options.Logger(defaultLogger),
}, options...)
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type nop struct{}
func (nop) Printf(_ string, _ ...interface{}) {}
func (nop) Println(_ ...interface{}) {}
func (nop) Status(_ context.Context) error { return nil }
func (nop) Starting() {}
func (nop) Healthy() {}
func (nop) Failing(_ error) {}
func (nop) Stopping() {}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
type simpleHealthCheck struct{ check healthCheckFunc }
func NewSimpleHealthCheck(check healthCheckFunc) HealthCheck { return &simpleHealthCheck{check: check} }
func (this simpleHealthCheck) Status(ctx context.Context) error { return this.check(ctx) }
type pingHealthCheck struct{ ping PingContext }
func NewPingHealthCheck(ping PingContext) HealthCheck { return &pingHealthCheck{ping: ping} }
func (this pingHealthCheck) Status(ctx context.Context) error { return this.ping.PingContext(ctx) }
type compositeHealthCheck []HealthCheck
func NewCompositeHealthCheck(checks ...HealthCheck) HealthCheck {
return compositeHealthCheck(checks)
}
func (this compositeHealthCheck) Status(ctx context.Context) error {
for _, item := range this {
if err := item.Status(ctx); err != nil {
return err
}
}
return nil
}
func (this compositeHealthCheck) Close() error {
for _, item := range this {
tryClose(item)
}
return nil
}