1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
timeout.go 1.41 KB
一键复制 编辑 原始数据 按行查看 历史
urso 提交于 2015-12-19 04:50 . Add support for filebeat multiline handling
package processor
import (
"errors"
"time"
)
var (
errTimeout = errors.New("timeout")
)
// timeoutProcessor will signal some configurable timeout error if no
// new line can be returned in time.
type timeoutProcessor struct {
reader LineProcessor
timeout time.Duration
signal error
running bool
ch chan lineMessage
}
type lineMessage struct {
line Line
err error
}
// newTimeoutProcessor returns a new timeoutProcessor from an input line processor.
func newTimeoutProcessor(in LineProcessor, signal error, timeout time.Duration) *timeoutProcessor {
if signal == nil {
signal = errTimeout
}
return &timeoutProcessor{
reader: in,
signal: signal,
timeout: timeout,
ch: make(chan lineMessage, 1),
}
}
// Next returns the next line. If no line was returned before timeout, the
// configured timeout error is returned.
// For handline timeouts a goroutine is started for reading lines from
// configured line processor. Only when underlying processor returns an error, the
// goroutine will be finished.
func (p *timeoutProcessor) Next() (Line, error) {
if !p.running {
p.running = true
go func() {
for {
line, err := p.reader.Next()
p.ch <- lineMessage{line, err}
if err != nil {
break
}
}
}()
}
select {
case msg := <-p.ch:
if msg.err != nil {
p.running = false
}
return msg.line, msg.err
case <-time.After(p.timeout):
return Line{}, p.signal
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v1.2.3

搜索帮助