-
Notifications
You must be signed in to change notification settings - Fork 1
/
health_impl.go
63 lines (50 loc) · 1.23 KB
/
health_impl.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
package hexa
import (
"context"
"github.com/kamva/hexa/hlog"
)
type Ping func(ctx context.Context) error
type pingHealth struct {
l hlog.Logger
identifier string
ping Ping
tags map[string]string
}
func NewPingHealth(l hlog.Logger, identifier string, ping Ping, tags map[string]string) Health {
return &pingHealth{
l: l,
identifier: identifier,
ping: ping,
tags: tags,
}
}
func (h *pingHealth) HealthIdentifier() string {
return h.identifier
}
func (h *pingHealth) LivenessStatus(ctx context.Context) LivenessStatus {
if err := h.ping(ctx); err != nil {
h.l.Error("can not ping", hlog.String("health_identifier", h.identifier), hlog.Err(err))
return StatusDead
}
return StatusAlive
}
func (h *pingHealth) ReadinessStatus(ctx context.Context) ReadinessStatus {
if h.LivenessStatus(ctx) == StatusAlive {
return StatusReady
}
return StatusUnReady
}
func (h *pingHealth) HealthStatus(ctx context.Context) HealthStatus {
liveness := h.LivenessStatus(ctx)
readiness := StatusReady
if liveness == StatusDead {
readiness = StatusUnReady
}
return HealthStatus{
Id: h.HealthIdentifier(),
Alive: liveness,
Ready: readiness,
Tags: h.tags,
}
}
var _ Health = &pingHealth{}