1 Star 1 Fork 0

yanpin-dev/propeller

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
checker.go 1.55 KB
一键复制 编辑 原始数据 按行查看 历史
zh_season 提交于 2021-03-17 18:26 . move package.
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/yanpin-dev/propeller.git
git@gitee.com:yanpin-dev/propeller.git
yanpin-dev
propeller
propeller
v0.3.5

搜索帮助