1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
runner.go 1.40 KB
一键复制 编辑 原始数据 按行查看 历史
package beater
import (
"sync"
"github.com/elastic/beats/libbeat/publisher"
)
// ModuleRunner is a facade for a ModuleWrapper that provides a simple interface
// for starting and stopping a Module.
type ModuleRunner interface {
// Start starts the Module. If Start is called more than once, only the
// first will start the Module.
Start()
// Stop stops the Module and waits for module's MetricSets to exit. The
// publisher.Client will be closed by Stop. If Stop is called more than
// once, only the first stop the Module and wait for it to exit.
Stop()
}
// NewModuleRunner returns a ModuleRunner facade. The events generated by
// the Module will be published to a new publisher.Client generated from the
// pubClientFactory.
func NewModuleRunner(pubClientFactory func() publisher.Client, mod *ModuleWrapper) ModuleRunner {
return &moduleRunner{
done: make(chan struct{}),
mod: mod,
client: pubClientFactory(),
}
}
type moduleRunner struct {
done chan struct{}
wg sync.WaitGroup
startOnce sync.Once
stopOnce sync.Once
mod *ModuleWrapper
client publisher.Client
}
func (mr *moduleRunner) Start() {
mr.startOnce.Do(func() {
output := mr.mod.Start(mr.done)
mr.wg.Add(1)
go func() {
defer mr.wg.Done()
PublishChannels(mr.client, output)
}()
})
}
func (mr *moduleRunner) Stop() {
mr.stopOnce.Do(func() {
close(mr.done)
mr.client.Close()
mr.wg.Wait()
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.1.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385