-
Notifications
You must be signed in to change notification settings - Fork 11
/
udp.go
101 lines (87 loc) · 2.11 KB
/
udp.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
package riemanngo
import (
"fmt"
"net"
"time"
"github.com/riemann/riemann-go-client/proto"
pb "google.golang.org/protobuf/proto"
"gopkg.in/tomb.v2"
)
// UDPClient is a type that implements the Client interface
type UDPClient struct {
addr string
conn net.Conn
requestQueue chan request
timeout time.Duration
t tomb.Tomb
}
// MaxUDPSize is the maximum allowed size of a UDP packet before automatically failing the send
const MaxUDPSize = 16384
// NewUDPClient - Factory
func NewUDPClient(addr string, timeout time.Duration) *UDPClient {
t := &UDPClient{
addr: addr,
requestQueue: make(chan request),
timeout: timeout,
}
return t
}
// Connect the udp client
func (c *UDPClient) Connect() error {
c.t.Go(func() error {
return c.runRequestQueue()
})
udp, err := net.DialTimeout("udp", c.addr, c.timeout)
if err != nil {
return err
}
c.conn = udp
return nil
}
// Send queues a request to send a message to the server
func (c *UDPClient) Send(message *proto.Msg) (*proto.Msg, error) {
responseCh := make(chan response)
c.requestQueue <- request{message, responseCh}
r := <-responseCh
return r.message, r.err
}
// Close will close the UDPClient
func (c *UDPClient) Close() error {
c.t.Kill(nil)
_ = c.t.Wait()
close(c.requestQueue)
err := c.conn.Close()
return err
}
// runRequestQueue services the UDPClient request queue
func (c *UDPClient) runRequestQueue() error {
for {
select {
case <-c.t.Dying():
return nil
case req := <-c.requestQueue:
message := req.message
responseCh := req.responseCh
msg, err := c.execRequest(message)
responseCh <- response{msg, err}
}
}
}
// execRequest will send a UDP message to Riemann
func (c *UDPClient) execRequest(message *proto.Msg) (*proto.Msg, error) {
err := c.conn.SetDeadline(time.Now().Add(c.timeout))
if err != nil {
return nil, err
}
data, err := pb.Marshal(message)
if err != nil {
return nil, err
}
if len(data) > MaxUDPSize {
return nil, fmt.Errorf("unable to send message, too large for udp")
}
if _, err = c.conn.Write(data); err != nil {
return nil, err
}
return nil, nil
}