/
types.go
101 lines (97 loc) · 2.48 KB
/
types.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
package services
import "context"
// Service represents a long-running service inside the Application.
//
// Typically, a Service will leverage utils.StateMachine to implement these
// calls in a safe manner.
//
// # Template
//
// Mockable Foo service with a run loop
//
// //go:generate mockery --quiet --name Foo --output ../internal/mocks/ --case=underscore
// type (
// // Expose a public interface so we can mock the service.
// Foo interface {
// service.Service
//
// // ...
// }
//
// foo struct {
// // ...
//
// stop chan struct{}
// done chan struct{}
//
// utils.StartStopOnce
// }
// )
//
// var _ Foo = (*foo)(nil)
//
// func NewFoo() Foo {
// f := &foo{
// // ...
// }
//
// return f
// }
//
// func (f *foo) Start(ctx context.Context) error {
// return f.StartOnce("Foo", func() error {
// go f.run()
//
// return nil
// })
// }
//
// func (f *foo) Close() error {
// return f.StopOnce("Foo", func() error {
// // trigger goroutine cleanup
// close(f.stop)
// // wait for cleanup to complete
// <-f.done
// return nil
// })
// }
//
// func (f *foo) run() {
// // signal cleanup completion
// defer close(f.done)
//
// for {
// select {
// // ...
// case <-f.stop:
// // stop the routine
// return
// }
// }
//
// }
type Service interface {
// Start the service.
// - Must return promptly if the context is cancelled.
// - Must not retain the context after returning (only applies to start-up)
// - Must not depend on external resources (no blocking network calls)
Start(context.Context) error
// Close stops the service.
// Invariants: Usually after this call the Service cannot be started
// again, you need to build a new Service to do so.
Close() error
HealthReporter
}
// HealthReporter should be implemented by any type requiring health checks.
type HealthReporter interface {
// Ready should return nil if ready, or an error message otherwise. From the k8s docs:
// > ready means it’s initialized and healthy means that it can accept traffic in kubernetes
// See: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/
Ready() error
// HealthReport returns a full health report of the callee including its dependencies.
// key is the dep name, value is nil if healthy, or error message otherwise.
// Use CopyHealth to collect reports from sub-services.
HealthReport() map[string]error
// Name returns the fully qualified name of the component. Usually the logger name.
Name() string
}