forked from redpanda-data/connect
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fan_out.go
181 lines (158 loc) · 5.11 KB
/
fan_out.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
179
180
181
// Copyright (c) 2014 Ashley Jeffs
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package broker
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"
"github.com/Jeffail/benthos/lib/util/throttle"
)
//------------------------------------------------------------------------------
// FanOut is a broker that implements types.Consumer and broadcasts each message
// out to an array of outputs.
type FanOut struct {
running int32
logger log.Modular
stats metrics.Type
throt *throttle.Type
transactions <-chan types.Transaction
outputTsChans []chan types.Transaction
outputResChans []chan types.Response
outputs []types.Output
outputNs []int
closedChan chan struct{}
closeChan chan struct{}
}
// NewFanOut creates a new FanOut type by providing outputs.
func NewFanOut(
outputs []types.Output, logger log.Modular, stats metrics.Type,
) (*FanOut, error) {
o := &FanOut{
running: 1,
stats: stats,
logger: logger.NewModule(".broker.fan_out"),
transactions: nil,
outputs: outputs,
outputNs: []int{},
closedChan: make(chan struct{}),
closeChan: make(chan struct{}),
}
o.throt = throttle.New(throttle.OptCloseChan(o.closeChan))
o.outputTsChans = make([]chan types.Transaction, len(o.outputs))
o.outputResChans = make([]chan types.Response, len(o.outputs))
for i := range o.outputTsChans {
o.outputNs = append(o.outputNs, i)
o.outputTsChans[i] = make(chan types.Transaction)
o.outputResChans[i] = make(chan types.Response)
if err := o.outputs[i].StartReceiving(o.outputTsChans[i]); err != nil {
return nil, err
}
}
return o, nil
}
//------------------------------------------------------------------------------
// StartReceiving assigns a new transactions channel for the broker to read.
func (o *FanOut) StartReceiving(transactions <-chan types.Transaction) error {
if o.transactions != nil {
return types.ErrAlreadyStarted
}
o.transactions = transactions
go o.loop()
return nil
}
//------------------------------------------------------------------------------
// loop is an internal loop that brokers incoming messages to many outputs.
func (o *FanOut) loop() {
defer func() {
for _, c := range o.outputTsChans {
close(c)
}
close(o.closedChan)
}()
for atomic.LoadInt32(&o.running) == 1 {
var ts types.Transaction
var open bool
select {
case ts, open = <-o.transactions:
if !open {
return
}
case <-o.closeChan:
return
}
o.stats.Incr("broker.fan_out.messages.received", 1)
outputTargets := o.outputNs
for len(outputTargets) > 0 {
for _, i := range outputTargets {
// Perform a copy here as it could be dangerous to release the
// same message to parallel processor pipelines.
msgCopy := ts.Payload.ShallowCopy()
select {
case o.outputTsChans[i] <- types.NewTransaction(msgCopy, o.outputResChans[i]):
case <-o.closeChan:
return
}
}
newTargets := []int{}
for _, i := range outputTargets {
select {
case res := <-o.outputResChans[i]:
if res.Error() != nil {
newTargets = append(newTargets, i)
o.logger.Errorf("Failed to dispatch fan out message: %v\n", res.Error())
o.stats.Incr("broker.fan_out.output.error", 1)
if !o.throt.Retry() {
return
}
} else {
o.throt.Reset()
o.stats.Incr("broker.fan_out.messages.sent", 1)
}
case <-o.closeChan:
return
}
}
outputTargets = newTargets
}
select {
case ts.ResponseChan <- types.NewSimpleResponse(nil):
case <-o.closeChan:
return
}
}
}
// CloseAsync shuts down the FanOut broker and stops processing requests.
func (o *FanOut) CloseAsync() {
if atomic.CompareAndSwapInt32(&o.running, 1, 0) {
close(o.closeChan)
}
}
// WaitForClose blocks until the FanOut broker has closed down.
func (o *FanOut) WaitForClose(timeout time.Duration) error {
select {
case <-o.closedChan:
case <-time.After(timeout):
return types.ErrTimeout
}
return nil
}
//------------------------------------------------------------------------------