/
tcp_server.go
284 lines (242 loc) · 6.91 KB
/
tcp_server.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package input
import (
"bufio"
"bytes"
"io"
"net"
"strings"
"sync/atomic"
"time"
"github.com/Jeffail/benthos/v3/internal/docs"
"github.com/Jeffail/benthos/v3/lib/log"
"github.com/Jeffail/benthos/v3/lib/message"
"github.com/Jeffail/benthos/v3/lib/metrics"
"github.com/Jeffail/benthos/v3/lib/types"
)
//------------------------------------------------------------------------------
func init() {
Constructors[TypeTCPServer] = TypeSpec{
constructor: fromSimpleConstructor(NewTCPServer),
Description: `
Creates a server that receives messages over TCP. Each connection is parsed as a
continuous stream of line delimited messages.
If multipart is set to false each line of data is read as a separate message. If
multipart is set to true each line is read as a message part, and an empty line
indicates the end of a message.
If the delimiter field is left empty then line feed (\n) is used.
The field ` + "`max_buffer`" + ` specifies the maximum amount of memory to
allocate _per connection_ for buffering lines of data. If a line of data from a
connection exceeds this value then the connection will be closed.`,
Status: docs.StatusDeprecated,
config: docs.FieldComponent().WithChildren(
docs.FieldCommon("address", ""),
docs.FieldCommon("multipart", ""),
docs.FieldCommon("max_buffer", ""),
docs.FieldCommon("delimiter", ""),
),
}
}
//------------------------------------------------------------------------------
// TCPServerConfig contains configuration for the TCPServer input type.
type TCPServerConfig struct {
Address string `json:"address" yaml:"address"`
Multipart bool `json:"multipart" yaml:"multipart"`
MaxBuffer int `json:"max_buffer" yaml:"max_buffer"`
Delim string `json:"delimiter" yaml:"delimiter"`
}
// NewTCPServerConfig creates a new TCPServerConfig with default values.
func NewTCPServerConfig() TCPServerConfig {
return TCPServerConfig{
Address: "127.0.0.1:0",
Multipart: false,
MaxBuffer: 1000000,
Delim: "",
}
}
//------------------------------------------------------------------------------
// TCPServer is an input type that binds to an address and consumes streams of
// messages over TCP.
type TCPServer struct {
running int32
conf TCPServerConfig
stats metrics.Type
log log.Modular
delim []byte
listener net.Listener
transactions chan types.Transaction
closeChan chan struct{}
closedChan chan struct{}
}
// NewTCPServer creates a new TCPServer input type.
func NewTCPServer(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
log.Warnln("The tcp_server input is deprecated, please use socket_server instead.")
ln, err := net.Listen("tcp", conf.TCPServer.Address)
if err != nil {
return nil, err
}
delim := []byte("\n")
if len(conf.TCPServer.Delim) > 0 {
delim = []byte(conf.TCPServer.Delim)
}
t := TCPServer{
running: 1,
conf: conf.TCPServer,
stats: stats,
log: log,
delim: delim,
listener: ln,
transactions: make(chan types.Transaction),
closeChan: make(chan struct{}),
closedChan: make(chan struct{}),
}
go t.loop()
return &t, nil
}
//------------------------------------------------------------------------------
// Addr returns the underlying TCP listeners address.
func (t *TCPServer) Addr() net.Addr {
return t.listener.Addr()
}
func (t *TCPServer) newScanner(r io.Reader) *bufio.Scanner {
scanner := bufio.NewScanner(r)
if t.conf.MaxBuffer != bufio.MaxScanTokenSize {
scanner.Buffer([]byte{}, t.conf.MaxBuffer)
}
scanner.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
}
if i := bytes.Index(data, t.delim); i >= 0 {
// We have a full terminated line.
return i + len(t.delim), data[0:i], nil
}
// If we're at EOF, we have a final, non-terminated line. Return it.
if atEOF {
return len(data), data, nil
}
// Request more data.
return 0, nil, nil
})
return scanner
}
func (t *TCPServer) loop() {
var (
mCount = t.stats.GetCounter("count")
mRcvd = t.stats.GetCounter("batch.received")
mPartsRcvd = t.stats.GetCounter("received")
mLatency = t.stats.GetTimer("latency")
)
defer func() {
atomic.StoreInt32(&t.running, 0)
if t.listener != nil {
t.listener.Close()
}
close(t.transactions)
close(t.closedChan)
}()
t.log.Infof("Receiving TCP messages from address: %v\n", t.listener.Addr())
sendMsg := func(msg types.Message) error {
tStarted := time.Now()
mPartsRcvd.Incr(int64(msg.Len()))
mRcvd.Incr(1)
resChan := make(chan types.Response)
select {
case t.transactions <- types.NewTransaction(msg, resChan):
case <-t.closeChan:
return types.ErrTypeClosed
}
select {
case res, open := <-resChan:
if !open {
return types.ErrTypeClosed
}
if res != nil {
if res.Error() != nil {
return res.Error()
}
}
case <-t.closeChan:
return types.ErrTypeClosed
}
mLatency.Timing(time.Since(tStarted).Nanoseconds())
return nil
}
go func() {
for {
conn, err := t.listener.Accept()
if err != nil {
if !strings.Contains(err.Error(), "use of closed network connection") {
t.log.Errorf("Failed to accept TCP connection: %v\n", err)
}
return
}
go func(c net.Conn) {
defer c.Close()
scanner := t.newScanner(c)
var msg types.Message
msgLoop := func() {
for msg != nil {
sendErr := sendMsg(msg)
if sendErr == nil || sendErr == types.ErrTypeClosed {
msg = nil
return
}
t.log.Errorf("Failed to send message: %v\n", sendErr)
<-time.After(time.Second)
}
}
for scanner.Scan() {
mCount.Incr(1)
if len(scanner.Bytes()) == 0 {
if t.conf.Multipart && msg != nil {
msgLoop()
}
continue
}
if msg == nil {
msg = message.New(nil)
}
msg.Append(message.NewPart(scanner.Bytes()))
if !t.conf.Multipart {
msgLoop()
}
}
if msg != nil {
msgLoop()
}
if cerr := scanner.Err(); cerr != nil {
if cerr != io.EOF {
t.log.Errorf("Connection error due to: %v\n", cerr)
}
}
}(conn)
}
}()
<-t.closeChan
}
// TransactionChan returns a transactions channel for consuming messages from
// this input.
func (t *TCPServer) TransactionChan() <-chan types.Transaction {
return t.transactions
}
// Connected returns a boolean indicating whether this input is currently
// connected to its target.
func (t *TCPServer) Connected() bool {
return true
}
// CloseAsync shuts down the TCPServer input and stops processing requests.
func (t *TCPServer) CloseAsync() {
if atomic.CompareAndSwapInt32(&t.running, 1, 0) {
close(t.closeChan)
}
}
// WaitForClose blocks until the TCPServer input has closed down.
func (t *TCPServer) WaitForClose(timeout time.Duration) error {
select {
case <-t.closedChan:
case <-time.After(timeout):
return types.ErrTimeout
}
return nil
}
//------------------------------------------------------------------------------