2020-06-15 10:04:08 +00:00
|
|
|
package health
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
type checker struct {
|
|
|
|
componentChecks map[string]Check
|
|
|
|
}
|
|
|
|
|
|
|
|
type Checker interface {
|
|
|
|
RegisterCheck(component string, check Check) error
|
|
|
|
IsHealthy() Result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) RegisterCheck(component string, check Check) error {
|
|
|
|
if _, exists := c.componentChecks[component]; exists {
|
2020-12-26 13:11:49 +00:00
|
|
|
return fmt.Errorf("component: %s: %w", component, ErrCheckForComponentAlreadyRegistered)
|
2020-06-15 10:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.componentChecks[component] = check
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *checker) IsHealthy() (r Result) {
|
|
|
|
r.Status = HEALTHY
|
|
|
|
r.Components = make(map[string]CheckResult)
|
|
|
|
for component, componentCheck := range c.componentChecks {
|
|
|
|
r.Components[component] = componentCheck()
|
|
|
|
r.Status = max(r.Components[component].Status, r.Status)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-26 13:11:49 +00:00
|
|
|
func max(s1, s2 Status) Status {
|
2020-06-15 10:04:08 +00:00
|
|
|
var max Status
|
|
|
|
if s1 > s2 {
|
|
|
|
max = s1
|
|
|
|
} else {
|
|
|
|
max = s2
|
|
|
|
}
|
|
|
|
|
|
|
|
if max > UNHEALTHY {
|
|
|
|
return UNHEALTHY
|
|
|
|
}
|
|
|
|
return max
|
|
|
|
}
|