-
Notifications
You must be signed in to change notification settings - Fork 785
/
output.go
83 lines (72 loc) · 1.66 KB
/
output.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
71
72
73
74
75
76
77
78
79
80
81
82
83
package tracing
import (
"context"
"sync/atomic"
"github.com/Jeffail/shutdown"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/internal/message"
)
type tracedOutput struct {
e *events
ctr *uint64
wrapped output.Streamed
tChan chan message.Transaction
shutSig *shutdown.Signaller
}
func traceOutput(e *events, ctr *uint64, i output.Streamed) output.Streamed {
t := &tracedOutput{
e: e,
ctr: ctr,
wrapped: i,
tChan: make(chan message.Transaction),
shutSig: shutdown.NewSignaller(),
}
return t
}
func (t *tracedOutput) UnwrapOutput() output.Streamed {
return t.wrapped
}
func (t *tracedOutput) loop(inChan <-chan message.Transaction) {
defer close(t.tChan)
for {
var tran message.Transaction
var open bool
select {
case tran, open = <-inChan:
if !open {
return
}
case <-t.shutSig.HardStopChan():
return
}
if t.e.IsEnabled() {
_ = tran.Payload.Iter(func(i int, part *message.Part) error {
_ = atomic.AddUint64(t.ctr, 1)
t.e.Add(EventConsumeOf(part))
return nil
})
}
select {
case t.tChan <- tran:
case <-t.shutSig.HardStopChan():
// Stop flushing if we fully timed out
return
}
}
}
func (t *tracedOutput) Consume(inChan <-chan message.Transaction) error {
go t.loop(inChan)
return t.wrapped.Consume(t.tChan)
}
func (t *tracedOutput) Connected() bool {
return t.wrapped.Connected()
}
func (t *tracedOutput) TriggerCloseNow() {
t.wrapped.TriggerCloseNow()
t.shutSig.TriggerHardStop()
}
func (t *tracedOutput) WaitForClose(ctx context.Context) error {
err := t.wrapped.WaitForClose(ctx)
t.shutSig.TriggerHardStop()
return err
}