-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpacket.go
62 lines (54 loc) · 987 Bytes
/
packet.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
package main
import (
"bytes"
"io"
"log"
"net"
)
type packet struct {
Length int
Buffer []byte
}
func (p *packet) handle(conn *net.UDPConn) {
buffer := bytes.NewBuffer(p.Buffer[:p.Length])
var pos int
for {
// read the next command
line, err := buffer.ReadBytes('\n')
if err != nil && err != io.EOF {
log.Fatal(err)
}
if len(line) == 0 {
break
}
if err != io.EOF {
line = line[:len(line)-1]
}
// read the key
metric, err := bytes.NewBuffer(line).ReadBytes(':')
if err != nil && err != io.EOF {
log.Fatal(err)
}
key := string(metric[:len(metric)-1])
// get the client
name, err := cons.Get(key)
if err != nil {
log.Fatal(err)
}
n, found := clientMap[name]
if !found {
log.Fatal("unknown client for key", key)
}
// write to the statsd server
_, err = conn.WriteToUDP(line, &n.Addr)
if err != nil {
n.Remove()
continue
}
// check position
pos += len(line)
if pos == p.Length {
break
}
}
}