-
Notifications
You must be signed in to change notification settings - Fork 31
/
agent.go
80 lines (69 loc) · 1.4 KB
/
agent.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
/*
Copyright <holder> All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
History:
Date Who ID Description
-------- --- --- -----------
01/13/19 nanjj Initial code
*/
package logs
import (
"net"
"github.com/uber/jaeger-client-go/utils"
grpc "google.golang.org/grpc"
"google.golang.org/grpc/codes"
)
type traceAgent struct {
queue chan []byte
}
func RegisterAgent(server *grpc.Server, listenAddr string) {
ta := &traceAgent{
queue: make(chan []byte, 1024),
}
go ta.ServeUDP(listenAddr)
RegisterTraceAgentAdminServer(server, ta)
}
func (ta *traceAgent) List(req *ListRequest, rep TraceAgentAdmin_ListServer) (err error) {
for {
b := <-ta.queue
if len(b) == 0 {
continue
}
err = rep.Send(&ListReply{Data: b})
if err != nil {
err = grpc.Errorf(codes.Aborted, "%v", err)
return
}
}
return
}
func (ta *traceAgent) ServeUDP(listenAddr string) {
pc, err := net.ListenPacket("udp", listenAddr)
if err != nil {
return
}
defer pc.Close()
for {
b := make([]byte, utils.UDPPacketMaxLength)
n, _, err := pc.ReadFrom(b)
if err != nil {
continue
}
if n == 0 {
continue
}
b = b[0:n]
ta.queue <- b
}
}
func RunAgent(listenAddr string) (err error) {
server := grpc.NewServer()
RegisterAgent(server, listenAddr)
var listen net.Listener
listen, err = net.Listen("tcp", listenAddr)
if err != nil {
return
}
err = server.Serve(listen)
return
}