/
udp_server.go
259 lines (219 loc) · 6.08 KB
/
udp_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
package input
import (
"bufio"
"bytes"
"io"
"net"
"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[TypeUDPServer] = TypeSpec{
constructor: fromSimpleConstructor(NewUDPServer),
Description: `
Creates a server that receives messages over UDP as a continuous stream of data.
Each line is interpretted as an individual 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 for buffering lines of data, this must exceed the largest expected
message size.`,
Status: docs.StatusDeprecated,
config: docs.FieldComponent().WithChildren(
docs.FieldCommon("address", ""),
docs.FieldCommon("max_buffer", ""),
docs.FieldCommon("delimiter", ""),
),
}
}
//------------------------------------------------------------------------------
// UDPServerConfig contains configuration for the UDPServer input type.
type UDPServerConfig struct {
Address string `json:"address" yaml:"address"`
MaxBuffer int `json:"max_buffer" yaml:"max_buffer"`
Delim string `json:"delimiter" yaml:"delimiter"`
}
// NewUDPServerConfig creates a new UDPServerConfig with default values.
func NewUDPServerConfig() UDPServerConfig {
return UDPServerConfig{
Address: "127.0.0.1:0",
MaxBuffer: 1000000,
Delim: "",
}
}
//------------------------------------------------------------------------------
// UDPServer is an input type that binds to an address and consumes streams of
// messages over UDP.
type UDPServer struct {
running int32
conf UDPServerConfig
stats metrics.Type
log log.Modular
delim []byte
conn net.PacketConn
transactions chan types.Transaction
closeChan chan struct{}
closedChan chan struct{}
}
// NewUDPServer creates a new UDPServer input type.
func NewUDPServer(conf Config, mgr types.Manager, log log.Modular, stats metrics.Type) (Type, error) {
pc, err := net.ListenPacket("udp", conf.UDPServer.Address)
if err != nil {
return nil, err
}
delim := []byte("\n")
if len(conf.UDPServer.Delim) > 0 {
delim = []byte(conf.UDPServer.Delim)
}
t := UDPServer{
running: 1,
conf: conf.UDPServer,
stats: stats,
log: log,
delim: delim,
conn: pc,
transactions: make(chan types.Transaction),
closeChan: make(chan struct{}),
closedChan: make(chan struct{}),
}
go t.loop()
return &t, nil
}
//------------------------------------------------------------------------------
// Addr returns the underlying UDP listeners address.
func (t *UDPServer) Addr() net.Addr {
return t.conn.LocalAddr()
}
func (t *UDPServer) newScanner(r net.PacketConn) *bufio.Scanner {
scanner := bufio.NewScanner(&wrapPacketConn{PacketConn: 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 *UDPServer) 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.conn != nil {
t.conn.Close()
}
close(t.transactions)
close(t.closedChan)
}()
t.log.Infof("Receiving UDP messages from address: %v\n", t.conn.LocalAddr())
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() {
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 := t.newScanner(t.conn)
for scanner.Scan() {
mCount.Incr(1)
if len(scanner.Bytes()) == 0 {
continue
}
if msg == nil {
msg = message.New(nil)
}
msg.Append(message.NewPart(scanner.Bytes()))
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)
}
}
}
}()
<-t.closeChan
}
// TransactionChan returns a transactions channel for consuming messages from
// this input.
func (t *UDPServer) TransactionChan() <-chan types.Transaction {
return t.transactions
}
// Connected returns a boolean indicating whether this input is currently
// connected to its target.
func (t *UDPServer) Connected() bool {
return true
}
// CloseAsync shuts down the UDPServer input and stops processing requests.
func (t *UDPServer) CloseAsync() {
if atomic.CompareAndSwapInt32(&t.running, 1, 0) {
close(t.closeChan)
}
}
// WaitForClose blocks until the UDPServer input has closed down.
func (t *UDPServer) WaitForClose(timeout time.Duration) error {
select {
case <-t.closedChan:
case <-time.After(timeout):
return types.ErrTimeout
}
return nil
}
//------------------------------------------------------------------------------