-
Notifications
You must be signed in to change notification settings - Fork 179
/
collector.go
98 lines (82 loc) · 2.26 KB
/
collector.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
package liveness
import (
"net/http"
"sync"
"time"
)
const (
// DefaultTolerance is the default time (in seconds) allowed between heartbeats.
DefaultTolerance = time.Second * 30
// ToleranceHeader is the HTTP header name used to override a collector's configured tolerance.
ToleranceHeader = "X-Liveness-Tolerance"
)
// CheckCollector produces multiple checks and returns
// live only if all decendant checks are live.
//
// Each child check may only be used by one goroutine,
// the CheckCollector may be used by multiple routines at once to
// produce checks.
type CheckCollector struct {
lock sync.RWMutex
defaultTolerance time.Duration
checks []Check
}
// NewCheckCollector creates a threadsafe Collector.
func NewCheckCollector(tolerance time.Duration) *CheckCollector {
if tolerance == 0 {
tolerance = DefaultTolerance
}
return &CheckCollector{
defaultTolerance: tolerance,
checks: make([]Check, 0, 1),
}
}
// NewCheck returns a Check which is safe for use on a single routine.
func (c *CheckCollector) NewCheck() Check {
c.lock.Lock()
check := &internalCheck{
defaultTolerance: c.defaultTolerance,
lastCheckIn: time.Now(),
}
c.checks = append(c.checks, check)
c.lock.Unlock()
return check
}
// Register adds a check to a collector
func (c *CheckCollector) Register(ck Check) {
c.lock.Lock()
c.checks = append(c.checks, ck)
c.lock.Unlock()
}
func (c *CheckCollector) ServeHTTP(w http.ResponseWriter, r *http.Request) {
tolerance := c.defaultTolerance
var err error
if toleranceStr := r.Header.Get(ToleranceHeader); toleranceStr != "" {
tolerance, err = time.ParseDuration(toleranceStr)
if err != nil {
http.Error(w, "Invalid tolerace: "+toleranceStr, http.StatusBadRequest)
return
}
}
if !c.IsLive(tolerance) {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
w.WriteHeader(http.StatusOK)
}
// IsLive checks if we are still live against the given the tolerace between hearbeats.
//
// If tolerance is 0, the default tolerance is used.
func (c *CheckCollector) IsLive(tolerance time.Duration) bool {
if tolerance == 0 {
tolerance = c.defaultTolerance
}
c.lock.RLock()
defer c.lock.RUnlock()
for i := range c.checks {
if !c.checks[i].IsLive(tolerance) {
return false
}
}
return true
}