-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
75 lines (65 loc) · 2.13 KB
/
main.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
package main
import (
"flag"
"fmt"
"net"
"os"
)
var maxPacketSize = flag.Int(
"max-line-length", 256, "Maximum line length for line protocol, in bytes",
)
var bufferSize = flag.Int(
"buffer-size", 4096, "Maximum number of packets that can be buffered",
)
var targetURL = flag.String(
"target-url", "http://127.0.0.1:8086/write?db=example", "URL where recieved data should be written",
)
var listenAddrStr = flag.String(
"listen-addr", "127.0.0.1:4444", "Local address for the UDP listener",
)
var attemptLimit = flag.Int(
"attempt-limit", 10, "Maximum number of times to retry failed influxdb requests (0=infinite)",
)
func main() {
flag.Parse()
listenAddr, err := net.ResolveUDPAddr("udp", *listenAddrStr)
if err != nil {
fmt.Fprintf(os.Stderr, "error resolving -listen-addr: %s", err)
os.Exit(1)
}
listenConn, err := net.ListenUDP("udp", listenAddr)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to listen on %s: %s", listenAddrStr, err)
os.Exit(2)
}
defer listenConn.Close()
// recvBufferQueue is a FIFO queue of "empty" Messages that are ready
// to be used to recieve data. At startup this buffer is filled with
// instances, which are taken by the reader and then returned to this
// queue by the writer once a message has been transmitted ot the backend.
recvBufferQueue := make(chan *Message, *bufferSize)
// sendBufferQueue is a FIFO queue of read Messages that are ready to be
// transmitted to the backend.
sendBufferQueue := make(chan *Message, *bufferSize)
// Allocate all of the message objects we'll use. By allocating these
// up front we avoid doing heap allocations after we initialize, thus
// reducing GC pressure.
// These all go into the recvBufferQueue where they can be read by
// the reader goroutine when it starts up.
for i := 0; i < *bufferSize; i++ {
msg := NewMessage(*maxPacketSize)
recvBufferQueue <- msg
}
reader := &Reader{
RecvBufferQueue: recvBufferQueue,
ListenConn: listenConn,
SendBufferQueue: sendBufferQueue,
}
writer := &Writer{
SendBufferQueue: sendBufferQueue,
TargetURL: *targetURL,
RecvBufferQueue: recvBufferQueue,
}
go reader.Run()
writer.Run()
}