代码拉取完成,页面将自动刷新
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
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。