forked from atlassian/gostatsd
/
batched_reader.go
77 lines (66 loc) · 1.37 KB
/
batched_reader.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
package statsd
import (
"net"
"runtime"
"golang.org/x/net/ipv6"
)
type Message struct {
Buffers [][]byte
Addr net.Addr
N int
}
type BatchReader interface {
ReadBatch(ms []Message) (int, error)
}
type V6BatchReader struct {
conn *ipv6.PacketConn
}
type GenericBatchReader struct {
conn net.PacketConn
}
func NewBatchReader(conn net.PacketConn) BatchReader {
if runtime.GOOS == "windows" {
return &GenericBatchReader{
conn: conn,
}
}
switch c := conn.(type) {
case *net.UDPConn:
return &V6BatchReader{
conn: ipv6.NewPacketConn(c),
}
default:
return &GenericBatchReader{
conn: conn,
}
}
}
func (br *V6BatchReader) ReadBatch(ms []Message) (int, error) {
ms6 := make([]ipv6.Message, len(ms))
for i, m := range ms {
ms6[i].Buffers = m.Buffers
}
count, err := br.conn.ReadBatch(ms6, 0)
if err != nil {
return 0, err
}
for i := 0; i < count; i++ {
ms[i].Addr = ms6[i].Addr
ms[i].N = ms6[i].N
}
return count, nil
}
func (gbr *GenericBatchReader) ReadBatch(ms []Message) (int, error) {
if len(ms) == 0 {
// This tends to happen in test code when the batch size is not set,
// if production code starts at all, then a panic won't be a hazard.
panic("attempt to read 0 packets")
}
nbytes, addr, err := gbr.conn.ReadFrom(ms[0].Buffers[0])
if err != nil {
return 0, err
}
ms[0].Addr = addr
ms[0].N = nbytes
return 1, nil
}