-
Notifications
You must be signed in to change notification settings - Fork 3
/
healthz.go
76 lines (62 loc) · 1.59 KB
/
healthz.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
package health
import (
"fmt"
"net/http"
"time"
"github.com/sirupsen/logrus"
)
type HealthzCfg struct {
// Max time between successful poll actions to consider cluster-controller alive
HealthyPollIntervalLimit time.Duration
// Max wait time for application to start
StartTimeLimit time.Duration
}
func NewHealthzProvider(cfg HealthzCfg, log logrus.FieldLogger) *HealthzProvider {
return &HealthzProvider{
log: log,
cfg: cfg,
}
}
type HealthzProvider struct {
log logrus.FieldLogger
cfg HealthzCfg
lastHealthyActionAt *time.Time
initStartedAt *time.Time
}
func (h *HealthzProvider) Check(_ *http.Request) (err error) {
defer func() {
if err != nil {
h.log.Warnf("Health check failed due to: %v", err)
}
}()
if h.lastHealthyActionAt != nil {
if time.Since(*h.lastHealthyActionAt) > h.cfg.HealthyPollIntervalLimit {
return fmt.Errorf("time since initialization or last poll action is over the considered healthy limit of %s", h.cfg.HealthyPollIntervalLimit)
}
return nil
}
if h.initStartedAt != nil {
if time.Since(*h.initStartedAt) > h.cfg.StartTimeLimit {
return fmt.Errorf("there was no sucessful poll action since start of application %s", h.cfg.StartTimeLimit)
}
return nil
}
return nil
}
func (h *HealthzProvider) Name() string {
return "action-health-check"
}
func (h *HealthzProvider) ActionPoll() {
h.lastHealthyActionAt = nowPtr()
h.initStartedAt = nil
}
func (h *HealthzProvider) Initializing() {
if h.initStartedAt == nil {
h.initStartedAt = nowPtr()
h.lastHealthyActionAt = nil
}
}
func nowPtr() *time.Time {
now := time.Now()
return &now
}