-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta.go
351 lines (288 loc) · 8.53 KB
/
meta.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
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
package process
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/derision-test/glock"
)
// Meta is a wrapper around a process value. This wrapper ensures that the configured
// receiver's methods are only called once and not called from an invalid state
// (e.g. Run called before Init or after a failed Init).
type Meta struct {
wrapped interface{}
options *metaOptions
logger Logger
mu sync.Mutex
initialized bool
running bool
stopping bool
stopped chan struct{}
}
var defaultClock = glock.NewRealClock()
func newMeta(wrapped interface{}, configs ...MetaConfigFunc) *Meta {
options := &metaOptions{
health: NewHealth(),
contextFilter: func(ctx context.Context) context.Context { return ctx },
logger: NilLogger,
initClock: defaultClock,
startupClock: defaultClock,
stopClock: defaultClock,
shutdownClock: defaultClock,
finalizeClock: defaultClock,
}
for _, f := range configs {
f(options)
}
return &Meta{
wrapped: wrapped,
options: options,
logger: options.logger.WithFields(options.metadata),
stopped: make(chan struct{}),
}
}
// Wrapped returns the underlying receiver.
func (m *Meta) Wrapped() interface{} {
return m.wrapped
}
// Name returns the process's configured name or `<unnamed {type}>` if one was not supplied.
func (m *Meta) Name() string {
if m.options.name == "" {
return fmt.Sprintf("<unnamed %T>", m.wrapped)
}
return m.options.name
}
// Metadata returns the process's configured metadata.
func (m *Meta) Metadata() map[string]interface{} {
return m.options.metadata
}
// Init invokes the wrapped value's Init method.
//
// A timeout error will be returned if the invocation does not unblock within the configured
// init timeout.
func (m *Meta) Init(ctx context.Context) (err error) {
defer func() {
if err == nil {
m.mu.Lock()
m.initialized = true
m.mu.Unlock()
}
}()
if initializer, ok := m.wrapped.(Initializer); ok {
return m.makeRunWithTimeout(ctx, "init", initializer.Init, m.options.initClock, m.options.initTimeout)
}
return nil
}
// Run invokes the wrapped value's Run method.
//
// A timeout error will be returned if the associated health instance does not report
// healthy within the configured startup timeout, or if Run method does not unblock
// after the meta instance's Stop method is called within the configured shutdown
// timeout.
//
// A canned error will also be returned if the underlying Run method unblocks with a
// nil value without Stop being called and the meta was not configured with the silent
// exit flag.
//
// This method will no-op if the meta instance was not initialized.
func (m *Meta) Run(ctx context.Context) error {
if runner, ok := m.wrapped.(Runner); ok && m.shouldRun() {
defer func() {
m.mu.Lock()
defer m.mu.Unlock()
m.running = false
}()
return m.run(ctx, runner)
}
return nil
}
func (m *Meta) shouldRun() bool {
m.mu.Lock()
defer m.mu.Unlock()
if !m.initialized || m.stopping {
return false
}
m.running = true
return true
}
func (m *Meta) run(ctx context.Context, runner Runner) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
result := runAsync(ctx, func(ctx context.Context) error {
return m.makeRunWithTimeout(ctx, "run", runner.Run, nil, 0)
})
healthStatusChannel, err := m.watchHealthStatus()
if err != nil {
return err
}
select {
case v := <-healthStatusChannel:
if !v {
return ErrStartupTimeout
}
select {
case err := <-result:
return m.handleResult(ctx, err)
case <-m.stopped:
cancel()
}
case err := <-result:
return m.handleResult(ctx, err)
case <-m.stopped:
cancel()
}
select {
case err := <-result:
return ignoreContextError(ctx, err)
case <-afterZeroUnbounded(m.options.shutdownClock, m.options.shutdownTimeout):
return ErrShutdownTimeout
}
}
// watchHealthStatus returns a channel that will receive the value true when the process
// becomes healthy, or false after the startup timeout has elapsed. If there are no health
// keys registered to this process then a nil channel is returned. Note that reading from
// a nil channel blocks forever.
func (m *Meta) watchHealthStatus() (chan bool, error) {
components, err := m.options.health.GetAll(m.options.healthKeys...)
if err != nil {
return nil, err
}
return m.watchHealthComponentStatus(components), nil
}
// watchHealthStatus returns a channel that will receive the value true when all of the
// given health components healthy, or false after the startup timeout has elapsed.
func (m *Meta) watchHealthComponentStatus(components []*HealthComponentStatus) chan bool {
timedOut := make(chan bool, 1)
go func() {
defer close(timedOut)
ch, cancel := m.options.health.Subscribe()
defer cancel()
for {
select {
case <-afterZeroUnbounded(m.options.startupClock, m.options.startupTimeout):
return
case <-ch:
healthy := true
for _, component := range components {
if !component.Healthy() {
healthy = false
break
}
}
if healthy {
timedOut <- true
return
}
}
}
}()
return timedOut
}
// handleResult is invoked after a value is received from the underlying run method.
// This will mark the meta instance as stopping, and determine the appropriate error
// value.
func (m *Meta) handleResult(ctx context.Context, err error) error {
m.mu.Lock()
stopping := m.stopping
m.mu.Unlock()
if err == nil && !stopping && !m.options.allowEarlyExit {
err = ErrUnexpectedReturn
}
return ignoreContextError(ctx, err)
}
// ignoreContextError returns nil if the given error is equal to the given context's
// underlying error and the given error otherwise. The given error may be wrapped.
func ignoreContextError(ctx context.Context, err error) error {
for ex := err; ex != nil; ex = errors.Unwrap(ex) {
if ex == ctx.Err() {
return nil
}
}
return err
}
// Stop invokes the wrapped value's Stop method.
//
// A timeout error will be returned if the invocation does not unblock within the configured
// stop timeout.
//
// This method will no-op if the meta instance is not currently running.
func (m *Meta) Stop(ctx context.Context) error {
if !m.shouldRunStop() {
return nil
}
defer close(m.stopped)
if stopper, ok := m.wrapped.(Stopper); ok {
return m.makeRunWithTimeout(ctx, "stop", stopper.Stop, m.options.stopClock, m.options.stopTimeout)
}
return nil
}
func (m *Meta) shouldRunStop() bool {
m.mu.Lock()
defer m.mu.Unlock()
if !m.initialized {
return false
}
m.stopping = true
return m.running
}
// Finalize invokes the wrapped value's Finalize method.
//
// A timeout error will be returned if the invocation does not unblock within the configured
// finalize timeout.
//
// This method will no-op if the meta instance was not initialized.
func (m *Meta) Finalize(ctx context.Context) error {
if finalizer, ok := m.wrapped.(Finalizer); ok && m.shouldRunFinalize() {
return m.makeRunWithTimeout(ctx, "finalize", finalizer.Finalize, m.options.finalizeClock, m.options.finalizeTimeout)
}
return nil
}
func (m *Meta) shouldRunFinalize() bool {
m.mu.Lock()
defer m.mu.Unlock()
return m.initialized
}
func (m *Meta) makeRunWithTimeout(ctx context.Context, opName string, fn func(ctx context.Context) error, clock glock.Clock, timeout time.Duration) error {
m.logger.Info("%s: %s starting", m.Name(), opName)
ctx, cancel := context.WithCancel(m.options.contextFilter(ctx))
defer cancel()
select {
case err := <-toStreamErrorFunc(fn)(ctx):
if err != nil {
return &opError{
source: err,
metaName: m.Name(),
opName: opName,
message: "failed",
}
}
m.logger.Info("%s: %s finished", m.Name(), opName)
return nil
case <-afterZeroUnbounded(clock, timeout):
return &opError{
source: nil,
metaName: m.Name(),
opName: opName,
message: "timeout",
}
}
}
// afterZeroUnbounded returns a channel that will receive a value after the given
// timeout. If the given timeout is zero, a nil channel will be returned. Note that
// reading from a nil channel blocks forever.
func afterZeroUnbounded(clock glock.Clock, timeout time.Duration) <-chan time.Time {
if timeout == 0 {
return nil
}
return clock.After(timeout)
}
// withTimeoutZeroUnbounded returns a context that will be cancelled after the given
// timeout, as well as a cleanup function. If the given timeout is zero, the context
// is returned unmodified.
func withTimeoutZeroUnbounded(ctx context.Context, clock glock.Clock, timeout time.Duration) (context.Context, context.CancelFunc) {
if timeout == 0 {
return ctx, func() {}
}
return glock.ContextWithTimeout(ctx, clock, timeout)
}