-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_checks.go
172 lines (146 loc) · 3.47 KB
/
api_checks.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package healthcheck
import (
"context"
"errors"
"github.com/kazhuravlev/healthcheck/internal/logr"
"time"
)
var (
_ ICheck = (*basicCheck)(nil)
_ ICheck = (*manualCheck)(nil)
_ ICheck = (*bgCheck)(nil)
)
// errInitial used as initial error for some checks.
var errInitial = errors.New("initial")
type basicCheck struct {
name string
ttl time.Duration
fn CheckFn
logg *logr.Ring
}
// NewBasic creates a basic check. This check will only be performed when RunAllChecks is called.
//
// hc, _ := healthcheck.New(...)
// hc.Register(healthcheck.NewBasic("postgres", time.Second, func(context.Context) error { ... }))
func NewBasic(name string, timeout time.Duration, fn CheckFn) *basicCheck {
return &basicCheck{
name: name,
ttl: timeout,
fn: fn,
logg: logr.New(),
}
}
func (c *basicCheck) id() string { return c.name }
func (c *basicCheck) timeout() time.Duration { return c.ttl }
func (c *basicCheck) check(ctx context.Context) logr.Rec {
res := logr.Rec{
Time: time.Now(),
Error: c.fn(ctx),
}
c.logg.Put(res)
return res
}
func (c *basicCheck) log() []logr.Rec {
return c.logg.SlicePrev()
}
type manualCheck struct {
name string
logg *logr.Ring
}
// NewManual create new check, that can be managed by client. Marked as failed by default.
//
// hc, _ := healthcheck.New(...)
// check := healthcheck.NewManual("some_subsystem")
// check.SetError(nil)
// hc.Register(check)
// check.SetError(errors.New("service unavailable"))
func NewManual(name string) *manualCheck {
check := &manualCheck{
name: name,
logg: logr.New(),
}
check.SetErr(errInitial)
return check
}
func (c *manualCheck) SetErr(err error) {
c.logg.Put(logr.Rec{
Time: time.Now(),
Error: err,
})
}
func (c *manualCheck) id() string { return c.name }
func (c *manualCheck) timeout() time.Duration { return time.Hour }
func (c *manualCheck) check(_ context.Context) logr.Rec {
rec, ok := c.logg.GetLast()
if !ok {
panic("manual check must have initial state")
}
return rec
}
func (c *manualCheck) log() []logr.Rec {
return c.logg.SlicePrev()
}
type bgCheck struct {
name string
period time.Duration
delay time.Duration
ttl time.Duration
fn CheckFn
logg *logr.Ring
}
// NewBackground will create a check that runs in background. Usually used for slow or expensive checks.
// Note: period should be greater than timeout.
//
// hc, _ := healthcheck.New(...)
// hc.Register(healthcheck.NewBackground("some_subsystem"))
func NewBackground(name string, initialErr error, delay, period, timeout time.Duration, fn CheckFn) *bgCheck {
check := &bgCheck{
name: name,
period: period,
delay: delay,
ttl: timeout,
fn: fn,
logg: logr.New(),
}
check.logg.Put(logr.Rec{
Time: time.Now(),
Error: initialErr,
})
return check
}
func (c *bgCheck) run() {
go func() {
time.Sleep(c.delay)
t := time.NewTimer(c.period)
defer t.Stop()
for {
func() {
ctx, cancel := context.WithTimeout(context.Background(), c.ttl)
defer cancel()
err := c.fn(ctx)
c.logg.Put(logr.Rec{
Time: time.Now(),
Error: err,
})
}()
select {
case <-t.C:
}
}
}()
}
func (c *bgCheck) id() string { return c.name }
func (c *bgCheck) timeout() time.Duration { return time.Hour }
func (c *bgCheck) check(_ context.Context) logr.Rec {
val, ok := c.logg.GetLast()
if !ok {
return logr.Rec{
Time: time.Now(),
Error: nil,
}
}
return val
}
func (c *bgCheck) log() []logr.Rec {
return c.logg.SlicePrev()
}