-
Notifications
You must be signed in to change notification settings - Fork 51
/
client.go
95 lines (84 loc) · 2.41 KB
/
client.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
package debugclient
import (
"context"
"errors"
"os"
"time"
"go.aporeto.io/trireme-lib/collector"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/controller/internal/enforcer/utils/rpcwrapper"
"go.aporeto.io/trireme-lib/controller/pkg/remoteenforcer/internal/statscollector"
"go.uber.org/zap"
)
const (
defaultDebugIntervalMilliseconds = 1000
debugContextID = "UNUSED"
debugRPCCommand = "StatsServer.PostPacketEvent"
)
type debugClient struct {
collector statscollector.Collector
rpchdl *rpcwrapper.RPCWrapper
secret string
debugChannel string
debugInterval time.Duration
stop chan bool
}
// NewDebugClient initializes a new Debug Client
func NewDebugClient(cr statscollector.Collector) (DebugClient, error) {
d := &debugClient{
collector: cr,
rpchdl: rpcwrapper.NewRPCWrapper(),
secret: os.Getenv(constants.EnvStatsSecret),
debugChannel: os.Getenv(constants.EnvStatsChannel),
debugInterval: defaultDebugIntervalMilliseconds * time.Millisecond,
stop: make(chan bool),
}
if d.debugChannel == "" {
return nil, errors.New("no path to debug socket provided")
}
if d.secret == "" {
return nil, errors.New("no secret provided for debug channel")
}
return d, nil
}
func (d *debugClient) sendData(records []*collector.PacketReport) error {
request := rpcwrapper.Request{
Payload: &rpcwrapper.DebugPacketPayload{
PacketRecords: records,
},
}
return d.rpchdl.RemoteCall(
debugContextID,
debugRPCCommand,
&request,
&rpcwrapper.Response{},
)
}
func (d *debugClient) sendPacketReports(ctx context.Context) {
ticker := time.NewTicker(d.debugInterval)
for {
select {
case <-ticker.C:
records := d.collector.GetAllDataPathPacketRecords()
if len(records) > 0 {
if err := d.sendData(records); err != nil {
zap.L().Debug("Unable to send debug packet reports", zap.Error(err))
}
}
case <-ctx.Done():
records := d.collector.GetAllDataPathPacketRecords()
if err := d.sendData(records); err != nil {
zap.L().Debug("Unable to send debug packet reports", zap.Error(err))
}
return
}
}
}
func (d *debugClient) Run(ctx context.Context) error {
if err := d.rpchdl.NewRPCClient(debugContextID, d.debugChannel, d.secret); err != nil {
zap.L().Error("Debug RPC client cannot connect", zap.Error(err))
return err
}
go d.sendPacketReports(ctx)
return nil
}