-
Notifications
You must be signed in to change notification settings - Fork 191
/
status.go
79 lines (65 loc) · 1.28 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
package health
import (
"fmt"
"log"
"strings"
"sync"
)
// todo: how to deal with multiple files?
// specified in zone and a status object for each?
type StatusType uint8
const (
StatusUnknown StatusType = iota
StatusUnhealthy
StatusHealthy
)
type Status interface {
GetStatus(string) StatusType
Reload() error
Close() error
}
type statusRegistry struct {
mu sync.RWMutex
m map[string]Status
}
var registry statusRegistry
type Service struct {
Status StatusType
}
func init() {
registry = statusRegistry{
m: make(map[string]Status),
}
}
func (r *statusRegistry) Add(name string, status Status) error {
r.mu.Lock()
defer r.mu.Unlock()
r.m[name] = status
return nil
}
func (st StatusType) String() string {
switch st {
case StatusHealthy:
return "healthy"
case StatusUnhealthy:
return "unhealthy"
case StatusUnknown:
return "unknown"
default:
return fmt.Sprintf("status=%d", st)
}
}
func GetStatus(name string) StatusType {
check := strings.SplitN(name, "/", 2)
if len(check) != 2 {
return StatusUnknown
}
registry.mu.RLock()
status, ok := registry.m[check[0]]
registry.mu.RUnlock()
log.Printf("looking up health for '%s', status register: '%s', found: %t", name, check[0], ok)
if !ok {
return StatusUnknown
}
return status.GetStatus(check[1])
}