forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout.go
70 lines (61 loc) · 1.41 KB
/
timeout.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
}
}