代码拉取完成,页面将自动刷新
package health
import (
"sync"
)
// Checker is a interface used to provide an indication of application health.
type Checker interface {
Name() string
Check() Health
}
// CompositeChecker aggregate a list of Checkers
type CompositeChecker struct {
checkers []Checker
info map[string]interface{}
}
// NewCompositeChecker creates a new CompositeChecker
func NewCompositeChecker() CompositeChecker {
return CompositeChecker{}
}
// AddInfo adds a info value to the Info map
func (c *CompositeChecker) AddInfo(key string, value interface{}) *CompositeChecker {
if c.info == nil {
c.info = make(map[string]interface{})
}
c.info[key] = value
return c
}
// AddChecker add a Checker to the aggregator
func (c *CompositeChecker) AddChecker(checker Checker) {
c.checkers = append(c.checkers, checker)
}
// Check returns the combination of all checkers added
// if some check is not up, the combined is marked as down
func (c CompositeChecker) Check() Health {
health := NewHealth()
health.Up()
healths := make(map[string]interface{})
type state struct {
h Health
name string
}
ch := make(chan state, len(c.checkers))
var wg sync.WaitGroup
for _, c := range c.checkers {
wg.Add(1)
go func(name string, f func() Health) {
ch <- state{h: f(), name: name}
wg.Done()
}(c.Name(), c.Check)
}
wg.Wait()
close(ch)
for s := range ch {
if !s.h.IsUp() && !health.IsDown() {
health.Down()
}
healths[s.name] = s.h
}
health.info = healths
// Extra Info
for key, value := range c.info {
health.AddInfo(key, value)
}
return health
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。