/
processor.go
178 lines (148 loc) · 4.44 KB
/
processor.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package pipeline
import (
"context"
"sync"
"time"
"github.com/benthosdev/benthos/v4/internal/component"
iprocessor "github.com/benthosdev/benthos/v4/internal/component/processor"
"github.com/benthosdev/benthos/v4/internal/message"
"github.com/benthosdev/benthos/v4/internal/old/util/throttle"
"github.com/benthosdev/benthos/v4/internal/shutdown"
)
//------------------------------------------------------------------------------
// Processor is a pipeline that supports both Consumer and Producer interfaces.
// The processor will read from a source, perform some processing, and then
// either propagate a new message or drop it.
type Processor struct {
msgProcessors []iprocessor.V1
messagesOut chan message.Transaction
responsesIn chan error
messagesIn <-chan message.Transaction
shutSig *shutdown.Signaller
}
// NewProcessor returns a new message processing pipeline.
func NewProcessor(msgProcessors ...iprocessor.V1) *Processor {
return &Processor{
msgProcessors: msgProcessors,
messagesOut: make(chan message.Transaction),
responsesIn: make(chan error),
shutSig: shutdown.NewSignaller(),
}
}
//------------------------------------------------------------------------------
// loop is the processing loop of this pipeline.
func (p *Processor) loop() {
defer func() {
// Signal all children to close.
for _, c := range p.msgProcessors {
c.CloseAsync()
}
close(p.messagesOut)
p.shutSig.ShutdownComplete()
}()
closeCtx, done := p.shutSig.CloseAtLeisureCtx(context.Background())
defer done()
var open bool
for !p.shutSig.ShouldCloseAtLeisure() {
var tran message.Transaction
select {
case tran, open = <-p.messagesIn:
if !open {
return
}
case <-p.shutSig.CloseAtLeisureChan():
return
}
resultMsgs, resultRes := iprocessor.ExecuteAll(p.msgProcessors, tran.Payload)
if len(resultMsgs) == 0 {
if err := tran.Ack(closeCtx, resultRes); err != nil && closeCtx.Err() != nil {
return
}
continue
}
if len(resultMsgs) > 1 {
p.dispatchMessages(closeCtx, resultMsgs, tran.Ack)
} else {
select {
case p.messagesOut <- message.NewTransactionFunc(resultMsgs[0], tran.Ack):
case <-p.shutSig.CloseAtLeisureChan():
return
}
}
}
}
// dispatchMessages attempts to send a multiple messages results of processors
// over the shared messages channel. This send is retried until success.
func (p *Processor) dispatchMessages(ctx context.Context, msgs []*message.Batch, ackFn func(context.Context, error) error) {
throt := throttle.New(throttle.OptCloseChan(p.shutSig.CloseAtLeisureChan()))
pending := msgs
for len(pending) > 0 {
wg := sync.WaitGroup{}
wg.Add(len(pending))
var newPending []*message.Batch
var newPendingMut sync.Mutex
for _, b := range pending {
b := b
transac := message.NewTransactionFunc(b, func(ctx context.Context, err error) error {
if err != nil {
newPendingMut.Lock()
newPending = append(newPending, b)
newPendingMut.Unlock()
}
wg.Done()
return nil
})
select {
case p.messagesOut <- transac:
case <-ctx.Done():
return
}
}
wg.Wait()
if pending = newPending; len(pending) > 0 && !throt.Retry() {
return
}
}
throt.Reset()
_ = ackFn(ctx, nil)
}
//------------------------------------------------------------------------------
// Consume assigns a messages channel for the pipeline to read.
func (p *Processor) Consume(msgs <-chan message.Transaction) error {
if p.messagesIn != nil {
return component.ErrAlreadyStarted
}
p.messagesIn = msgs
go p.loop()
return nil
}
// TransactionChan returns the channel used for consuming messages from this
// pipeline.
func (p *Processor) TransactionChan() <-chan message.Transaction {
return p.messagesOut
}
// CloseAsync shuts down the pipeline and stops processing messages.
func (p *Processor) CloseAsync() {
p.shutSig.CloseAtLeisure()
// Signal all children to close.
for _, c := range p.msgProcessors {
c.CloseAsync()
}
}
// WaitForClose blocks until the StackBuffer output has closed down.
func (p *Processor) WaitForClose(timeout time.Duration) error {
stopBy := time.Now().Add(timeout)
select {
case <-p.shutSig.HasClosedChan():
case <-time.After(time.Until(stopBy)):
return component.ErrTimeout
}
// Wait for all processors to close.
for _, c := range p.msgProcessors {
if err := c.WaitForClose(time.Until(stopBy)); err != nil {
return err
}
}
return nil
}
//------------------------------------------------------------------------------