-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
82 lines (68 loc) · 1.71 KB
/
api.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
package healthcheck
import (
"context"
"log/slog"
"sync"
)
// Register will register a check.
//
// All checks should have a name. Will be better that name will contain only lowercase symbols and lodash.
// This is allowing to have the same name for Check and for metrics.
func (s *Healthcheck) Register(ctx context.Context, check ICheck) {
s.checksMu.Lock()
defer s.checksMu.Unlock()
checkID, ok := name2id(check.id())
if !ok {
s.opts.logger.WarnContext(ctx, "choose a better name for check. see docs of Register method",
slog.String("name", check.id()),
slog.String("better_name", checkID))
}
CheckID:
for i := range s.checks {
if s.checks[i].ID == checkID {
newID := checkID + "_x"
s.opts.logger.WarnContext(ctx, "check name is duplicated. add prefix",
slog.String("name", check.id()),
slog.String("new_name", newID))
checkID = newID
goto CheckID
}
}
switch check := check.(type) {
case *bgCheck:
check.run()
}
s.checks = append(s.checks, checkContainer{
ID: checkID,
Check: check,
})
}
// RunAllChecks will run all check immediately.
func (s *Healthcheck) RunAllChecks(ctx context.Context) Report {
s.checksMu.RLock()
defer s.checksMu.RUnlock()
checks := make([]Check, len(s.checks))
{
wg := new(sync.WaitGroup)
wg.Add(len(s.checks))
// TODO(zhuravlev): do not run goroutines for checks like manual and bg check.
for i := range s.checks {
go func(i int, check checkContainer) {
defer wg.Done()
checks[i] = s.runCheck(ctx, check)
}(i, s.checks[i])
}
wg.Wait()
}
status := StatusUp
for _, check := range checks {
if check.State.Status == StatusDown {
status = StatusDown
break
}
}
return Report{
Status: status,
Checks: checks,
}
}