-
Notifications
You must be signed in to change notification settings - Fork 134
/
multi_checker.go
52 lines (44 loc) · 1.03 KB
/
multi_checker.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
package health
import (
"errors"
"fmt"
"strings"
"sync"
)
// MultiChecker combines multiple Checker instances.
// Calls to MultiChecker.Check() calls Check() on each constituent checker
// and returns a new error created by joining any errors returned from those calls,
// or nil if no errors are found.
type MultiChecker struct {
mu sync.Mutex
checkers []Checker
}
func NewMultiChecker(checkers ...Checker) *MultiChecker {
return &MultiChecker{
checkers: checkers,
}
}
func (mc *MultiChecker) Check() error {
mc.mu.Lock()
defer mc.mu.Unlock()
if len(mc.checkers) == 0 {
return fmt.Errorf("no checkers registered")
}
errorStrings := []string{}
for _, checker := range mc.checkers {
err := checker.Check()
if err != nil {
errorStrings = append(errorStrings, err.Error())
}
}
if len(errorStrings) == 0 {
return nil
} else {
return errors.New(strings.Join(errorStrings, "\n"))
}
}
func (mc *MultiChecker) Add(checker Checker) {
mc.mu.Lock()
defer mc.mu.Unlock()
mc.checkers = append(mc.checkers, checker)
}