forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.go
165 lines (140 loc) · 3.74 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 (
"sync/atomic"
"time"
"github.com/Jeffail/benthos/lib/types"
"github.com/Jeffail/benthos/lib/util/service/log"
"github.com/Jeffail/benthos/lib/util/service/metrics"
)
//------------------------------------------------------------------------------
// 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 {
running uint32
workers []Type
log log.Modular
stats metrics.Type
messagesIn <-chan types.Transaction
messagesOut chan types.Transaction
closeChan chan struct{}
closed chan struct{}
}
// NewPool returns a new pipeline pool that utilises multiple processor threads.
func NewPool(
constructor ConstructorFunc,
threads int,
log log.Modular,
stats metrics.Type,
) (*Pool, error) {
p := &Pool{
running: 1,
workers: make([]Type, threads),
log: log,
stats: stats,
messagesOut: make(chan types.Transaction),
closeChan: make(chan struct{}),
closed: make(chan struct{}),
}
for i := range p.workers {
var err error
if p.workers[i], err = constructor(); err != nil {
return nil, err
}
}
return p, nil
}
//------------------------------------------------------------------------------
// loop is the processing loop of this pipeline.
func (p *Pool) loop() {
defer func() {
atomic.StoreUint32(&p.running, 0)
// Signal all workers to close.
for _, worker := range p.workers {
worker.CloseAsync()
}
// Wait for all workers to be closed before closing our response and
// messages channels as the workers may still have access to them.
for _, worker := range p.workers {
err := worker.WaitForClose(time.Second)
for err != nil {
err = worker.WaitForClose(time.Second)
}
}
close(p.messagesOut)
close(p.closed)
}()
internalMessages := make(chan types.Transaction)
remainingWorkers := int64(len(p.workers))
for _, worker := range p.workers {
if err := worker.StartReceiving(p.messagesIn); err != nil {
p.log.Errorf("Failed to start pipeline worker: %v\n", err)
atomic.AddInt64(&remainingWorkers, -1)
continue
}
go func(w Type) {
defer func() {
if atomic.AddInt64(&remainingWorkers, -1) == 0 {
close(internalMessages)
}
}()
for {
t, open := <-w.TransactionChan()
if !open {
return
}
select {
case internalMessages <- t:
case <-p.closeChan:
return
}
}
}(worker)
}
for atomic.LoadUint32(&p.running) == 1 && atomic.LoadInt64(&remainingWorkers) > 0 {
select {
case t, open := <-internalMessages:
if !open {
return
}
select {
case p.messagesOut <- t:
case <-p.closeChan:
return
}
case <-p.closeChan:
return
}
}
}
//------------------------------------------------------------------------------
// StartReceiving assigns a messages channel for the pipeline to read.
func (p *Pool) StartReceiving(msgs <-chan types.Transaction) error {
if p.messagesIn != nil {
return types.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 types.Transaction {
return p.messagesOut
}
// CloseAsync shuts down the pipeline and stops processing messages.
func (p *Pool) CloseAsync() {
if atomic.CompareAndSwapUint32(&p.running, 1, 0) {
close(p.closeChan)
}
}
// WaitForClose - Blocks until the StackBuffer output has closed down.
func (p *Pool) WaitForClose(timeout time.Duration) error {
select {
case <-p.closed:
case <-time.After(timeout):
return types.ErrTimeout
}
return nil
}
//------------------------------------------------------------------------------