1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
util.go 2.12 KB
一键复制 编辑 原始数据 按行查看 历史
Ben Gadbois 提交于 2017-01-23 03:34 . Fix small spelling mistakes (#3434)
package flows
import (
"sync"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/packetbeat/publish"
)
type worker struct {
wg sync.WaitGroup
done chan struct{}
run func(*worker)
}
type spool struct {
pub publish.Flows
events []common.MapStr
}
func newWorker(fn func(w *worker)) *worker {
return &worker{
done: make(chan struct{}),
run: fn,
}
}
func (w *worker) Start() {
debugf("start flows worker")
w.wg.Add(1)
go func() {
defer w.finished()
w.run(w)
}()
}
func (w *worker) Stop() {
debugf("stop flows worker")
close(w.done)
w.wg.Wait()
debugf("stopped flows worker")
}
func (w *worker) finished() {
w.wg.Done()
logp.Info("flows worker loop stopped")
}
func (w *worker) sleep(d time.Duration) bool {
select {
case <-w.done:
return false
case <-time.After(d):
return true
}
}
func (w *worker) tick(t *time.Ticker) bool {
select {
case <-w.done:
return false
case <-t.C:
return true
}
}
func (w *worker) periodically(tick time.Duration, fn func() error) {
defer debugf("stop periodic loop")
ticker := time.NewTicker(tick)
for {
cont := w.tick(ticker)
if !cont {
return
}
err := fn()
if err != nil {
return
}
}
}
func (s *spool) init(pub publish.Flows, sz int) {
s.pub = pub
s.events = make([]common.MapStr, 0, sz)
}
func (s *spool) publish(event common.MapStr) {
s.events = append(s.events, event)
if len(s.events) == cap(s.events) {
s.flush()
}
}
func (s *spool) flush() {
if len(s.events) == 0 {
return
}
s.pub.PublishFlows(s.events)
s.events = make([]common.MapStr, 0, cap(s.events))
}
func gcd(a, b int64) int64 {
if a < 0 || b < 0 {
return 0
}
switch {
case a == b:
return a
case a == 0:
return b
case b == 0:
return a
}
shift := uint(0)
for (a&1) == 0 && (b&1) == 0 {
shift++
a /= 2
b /= 2
}
for (a & 1) == 0 {
a = a / 2
}
// a is always odd
for {
for (b & 1) == 0 {
b = b / 2
}
// both a and b are odd. guaranteed b >= a
if a > b {
a, b = b, a
}
b -= a
if b == 0 {
break
}
}
// restore common factors of 2
return a << shift
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.4.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385