1 Star 0 Fork 0

zhangjungang/beats

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
timeout.go 1.34 KB
一键复制 编辑 原始数据 按行查看 历史
package reader
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 Timeout struct {
reader Reader
timeout time.Duration
signal error
running bool
ch chan lineMessage
}
type lineMessage struct {
line Message
err error
}
// NewTimeout returns a new timeout reader from an input line reader.
func NewTimeout(reader Reader, signal error, t time.Duration) *Timeout {
if signal == nil {
signal = errTimeout
}
return &Timeout{
reader: reader,
signal: signal,
timeout: t,
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 reader. Only when underlying reader returns an error, the
// goroutine will be finished.
func (p *Timeout) Next() (Message, error) {
if !p.running {
p.running = true
go func() {
for {
message, err := p.reader.Next()
p.ch <- lineMessage{message, 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 Message{}, p.signal
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhangjungang/beats.git
git@gitee.com:zhangjungang/beats.git
zhangjungang
beats
beats
v5.2.1

搜索帮助