-
Notifications
You must be signed in to change notification settings - Fork 786
/
pool.go
165 lines (142 loc) · 3.99 KB
/
pool.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
package pipeline
import (
"context"
"runtime"
"sync"
"sync/atomic"
"github.com/benthosdev/benthos/v4/internal/component"
"github.com/benthosdev/benthos/v4/internal/component/processor"
"github.com/benthosdev/benthos/v4/internal/log"
"github.com/benthosdev/benthos/v4/internal/message"
"github.com/benthosdev/benthos/v4/internal/shutdown"
)
// Pool is a pool of pipelines. Each pipeline reads from a shared transaction
// channel. Inputs remain coupled to their outputs as they propagate the
// response channel in the transaction.
type Pool struct {
workers []processor.Pipeline
log log.Modular
messagesIn <-chan message.Transaction
messagesOut chan message.Transaction
shutSig *shutdown.Signaller
}
// NewPool creates a new processing pool.
func NewPool(threads int, log log.Modular, msgProcessors ...processor.V1) (*Pool, error) {
if threads <= 0 {
threads = runtime.NumCPU()
}
p := &Pool{
workers: make([]processor.Pipeline, threads),
log: log,
messagesOut: make(chan message.Transaction),
shutSig: shutdown.NewSignaller(),
}
for i := range p.workers {
p.workers[i] = NewProcessor(msgProcessors...)
}
return p, nil
}
//------------------------------------------------------------------------------
// loop is the processing loop of this pipeline.
func (p *Pool) loop() {
// Note this is currently kept open as we only have our children as a
// shutdown mechanism. This puts trust in individual processor pipelines, if
// that's not realistic we can consider adding a close now to the
// TriggerCloseNow method.
closeNowCtx, cnDone := p.shutSig.CloseNowCtx(context.Background())
defer cnDone()
defer func() {
for _, c := range p.workers {
if err := c.WaitForClose(closeNowCtx); err != nil {
break
}
}
close(p.messagesOut)
p.shutSig.ShutdownComplete()
}()
internalMessages := make(chan message.Transaction)
remainingWorkers := int64(len(p.workers))
var closeInternalOnce sync.Once
for _, worker := range p.workers {
if err := worker.Consume(p.messagesIn); err != nil {
p.log.Errorf("Failed to start pipeline worker: %v\n", err)
atomic.AddInt64(&remainingWorkers, -1)
continue
}
go func(w processor.Pipeline) {
defer func() {
if v := atomic.AddInt64(&remainingWorkers, -1); v <= 0 {
closeInternalOnce.Do(func() {
close(internalMessages)
})
}
}()
for {
var t message.Transaction
var open bool
select {
case t, open = <-w.TransactionChan():
if !open {
return
}
case <-p.shutSig.CloseNowChan():
return
}
select {
case internalMessages <- t:
case <-p.shutSig.CloseNowChan():
return
}
}
}(worker)
}
for atomic.LoadInt64(&remainingWorkers) > 0 {
select {
case t, open := <-internalMessages:
if !open {
return
}
select {
case p.messagesOut <- t:
case <-p.shutSig.CloseNowChan():
return
}
case <-p.shutSig.CloseNowChan():
return
}
}
}
//------------------------------------------------------------------------------
// Consume assigns a messages channel for the pipeline to read.
func (p *Pool) 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 *Pool) TransactionChan() <-chan message.Transaction {
return p.messagesOut
}
// TriggerCloseNow signals that the component should close immediately,
// messages in flight will be dropped.
func (p *Pool) TriggerCloseNow() {
for _, w := range p.workers {
w.TriggerCloseNow()
}
}
// WaitForClose blocks until the component has closed down or the context is
// cancelled. Closing occurs either when the input transaction channel is
// closed and messages are flushed (and acked), or when CloseNowAsync is
// called.
func (p *Pool) WaitForClose(ctx context.Context) error {
select {
case <-p.shutSig.HasClosedChan():
case <-ctx.Done():
return ctx.Err()
}
return nil
}