-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflow_queue.go
126 lines (107 loc) · 2.82 KB
/
flow_queue.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package api
import (
"context"
"fmt"
"time"
"github.com/evanrolfe/trayce_agent/internal/sockets"
)
type FlowQueue struct {
grpcClient TrayceAgentClient
flows []*Flow
batchSize int
}
func NewFlowQueue(grpcClient TrayceAgentClient, batchSize int) *FlowQueue {
return &FlowQueue{
grpcClient: grpcClient,
batchSize: batchSize,
}
}
func (fq *FlowQueue) Start(ctx context.Context, inputChan chan sockets.Flow) {
// Listen to the input channel and queue any flows received from it
go func(ctx context.Context) {
for {
select {
case <-ctx.Done():
fmt.Println("[FlowQueue] stopping receiver go-routine")
return
case flow := <-inputChan:
fmt.Println("[FlowQueue] received flow", flow.UUID, "req:", len(flow.Request), "resp:", len(flow.Response))
// Convert socket.Flow to Flow
apiFlow := &Flow{
Uuid: flow.UUID,
SourceAddr: flow.SourceAddr,
DestAddr: flow.DestAddr,
L4Protocol: flow.L4Protocol,
L7Protocol: flow.L7Protocol,
Request: flow.Request,
Response: flow.Response,
}
// Queue the Flow
fq.flows = append(fq.flows, apiFlow)
if len(fq.flows)%10 == 0 {
fmt.Println("[FlowQueue] received", len(fq.flows), "flows")
}
}
}
}(ctx)
go func(ctx context.Context) {
for {
select {
case <-time.After(100 * time.Millisecond): // Wait for 100ms
fq.processQueue()
case <-ctx.Done(): // Check if the context has been cancelled
fmt.Println("[FlowQueue] stopping processQueue go-routine")
return // Exit the loop (and possibly the goroutine)
}
}
}(ctx)
fmt.Println("[FlowQueue] running...")
}
// TODO: Sometimes this doesn't process the flows in order, possibly because the FlowQueue is instantiated
// inside another go routine so there are other flow queues running?
func (fq *FlowQueue) processQueue() {
if len(fq.flows) == 0 {
return
}
flows := fq.shiftQueue(fq.batchSize)
apiFlows := &Flows{Flows: flows}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
fmt.Println("[FlowQueue] sending flows", len(apiFlows.Flows))
_, err := fq.grpcClient.SendFlowsObserved(ctx, apiFlows)
if err != nil {
fmt.Println("[ERROR] could not request:", err)
}
}
func (fq *FlowQueue) shiftQueue(n int) []*Flow {
if len(fq.flows) < n {
n = len(fq.flows)
}
flows := fq.flows[0:n]
fq.flows = fq.flows[n:]
return flows
}
func (fq *FlowQueue) clearQueue() {
fq.flows = []*Flow{}
}
// func test() {
// for keepGoing := true; keepGoing; {
// var batch []string
// expire := time.After(maxTimeout)
// for {
// select {
// case value, ok := <-values:
// if !ok {
// keepGoing = false
// goto done
// }
// batch = append(batch, value)
// if len(batch) == maxItems {
// goto done
// }
// case <-expire:
// goto done
// }
// }
// }
// }