-
Notifications
You must be signed in to change notification settings - Fork 2
/
health.go
123 lines (100 loc) · 2.75 KB
/
health.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
package process
import (
"fmt"
"sync"
)
// Health is an aggregate container reporting the current health status of
// individual application components.
type Health struct {
mu sync.Mutex
components map[interface{}]*HealthComponentStatus
subscribers []chan<- struct{}
}
// NewHealth creates an empty Health instance.
func NewHealth() *Health {
return &Health{
components: map[interface{}]*HealthComponentStatus{},
}
}
// Healthy returns true if all registered components are healthy.
func (h *Health) Healthy() bool {
h.mu.Lock()
defer h.mu.Unlock()
for _, component := range h.components {
if !component.healthy {
return false
}
}
return true
}
// Subscribe returns a notification channel that receives a value whenever
// the set of components change, or the status of a component changes. This
// method also returns a cancellation function that should be called once
// the user wishes to unsubscribe.
func (h *Health) Subscribe() (<-chan struct{}, func()) {
h.mu.Lock()
defer h.mu.Unlock()
n := 0
for n < len(h.subscribers) && h.subscribers[n] != nil {
n++
}
ch := make(chan struct{}, 1)
ch <- struct{}{}
h.subscribers = append(h.subscribers, ch)
unsubscribe := func() {
h.mu.Lock()
h.subscribers[n] = nil
h.mu.Unlock()
}
var once sync.Once
return ch, func() { once.Do(unsubscribe) }
}
// Get returns the component status value registered to the given key.
func (h *Health) Get(key interface{}) (*HealthComponentStatus, bool) {
h.mu.Lock()
defer h.mu.Unlock()
component, ok := h.components[key]
return component, ok
}
// GetAll returns the component status values registered to the given keys.
func (h *Health) GetAll(keys ...interface{}) ([]*HealthComponentStatus, error) {
if len(keys) == 0 {
return nil, nil
}
h.mu.Lock()
defer h.mu.Unlock()
components := make([]*HealthComponentStatus, 0, len(keys))
for _, key := range keys {
component, ok := h.components[key]
if !ok {
return nil, fmt.Errorf("health component %q not registered", key)
}
components = append(components, component)
}
return components, nil
}
// Register creates and returns a new component status value for the given key.
// It an error to register the same key twice.
func (h *Health) Register(key interface{}) (*HealthComponentStatus, error) {
h.mu.Lock()
defer h.mu.Unlock()
if _, ok := h.components[key]; ok {
return nil, ErrHealthComponentAlreadyRegistered
}
component := newHealthComponentStatus(h, key)
h.components[key] = component
h.notify()
return component, nil
}
// notify writes a signal to all subscribed channels. Callers MUST lock b.mu.
func (h *Health) notify() {
for _, subscriber := range h.subscribers {
if subscriber == nil {
continue
}
select {
case subscriber <- struct{}{}:
default:
}
}
}