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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package main
import (
"context"
log "github.com/sirupsen/logrus"
"time"
)
const InitialPayloadsSize = 1024
const DefaultQueryFormatLimit = 8024
const InitialRegexSlicesCount = 8
type ConnectionStream struct {
ID RowID `bson:"_id"`
ConnectionID RowID `bson:"connection_id"`
FromClient bool `bson:"from_client"`
DocumentIndex int `bson:"document_index"`
Payload []byte `bson:"payload"`
BlocksIndexes []int `bson:"blocks_indexes"`
BlocksTimestamps []time.Time `bson:"blocks_timestamps"`
BlocksLoss []bool `bson:"blocks_loss"`
PatternMatches map[uint][]PatternSlice `bson:"pattern_matches"`
}
type PatternSlice [2]uint64
type Payload struct {
FromClient bool `json:"from_client"`
Content string `json:"content"`
Index int `json:"index"`
Timestamp time.Time `json:"timestamp"`
IsRetransmitted bool `json:"is_retransmitted"`
RegexMatches []RegexSlice `json:"regex_matches"`
}
type RegexSlice struct {
From uint64 `json:"from"`
To uint64 `json:"to"`
}
type QueryFormat struct {
Format string `form:"format"`
Skip uint64 `form:"skip"`
Limit uint64 `form:"limit"`
}
type ConnectionStreamsController struct {
storage Storage
}
func NewConnectionStreamsController(storage Storage) ConnectionStreamsController {
return ConnectionStreamsController{
storage: storage,
}
}
func (csc ConnectionStreamsController) GetConnectionPayload(c context.Context, connectionID RowID,
format QueryFormat) []Payload {
payloads := make([]Payload, 0, InitialPayloadsSize)
var clientIndex, serverIndex, globalIndex uint64
if format.Limit <= 0 {
format.Limit = DefaultQueryFormatLimit
}
var clientBlocksIndex, serverBlocksIndex int
var clientDocumentIndex, serverDocumentIndex int
clientStream := csc.getConnectionStream(c, connectionID, true, clientDocumentIndex)
serverStream := csc.getConnectionStream(c, connectionID, false, serverDocumentIndex)
hasClientBlocks := func() bool {
return clientBlocksIndex < len(clientStream.BlocksIndexes)
}
hasServerBlocks := func() bool {
return serverBlocksIndex < len(serverStream.BlocksIndexes)
}
var payload Payload
for !clientStream.ID.IsZero() || !serverStream.ID.IsZero() {
if hasClientBlocks() && !(hasServerBlocks() && // next payload is from client
clientStream.BlocksTimestamps[0].UnixNano() > serverStream.BlocksTimestamps[0].UnixNano()) {
start := clientStream.BlocksIndexes[clientBlocksIndex]
end := 0
if clientBlocksIndex < len(clientStream.BlocksIndexes)-1 {
end = clientStream.BlocksIndexes[clientBlocksIndex+1]
} else {
end = len(clientStream.Payload) - 1
}
size := uint64(end - start)
payload = Payload{
FromClient: true,
Content: DecodeBytes(clientStream.Payload[start:end], format.Format),
Index: start,
Timestamp: clientStream.BlocksTimestamps[clientBlocksIndex],
IsRetransmitted: clientStream.BlocksLoss[clientBlocksIndex],
RegexMatches: findMatchesBetween(clientStream.PatternMatches, clientIndex, clientIndex+size),
}
clientIndex += size
globalIndex += size
clientBlocksIndex++
} else { // next payload is from server
start := serverStream.BlocksIndexes[serverBlocksIndex]
end := 0
if serverBlocksIndex < len(serverStream.BlocksIndexes)-1 {
end = serverStream.BlocksIndexes[serverBlocksIndex+1]
} else {
end = len(serverStream.Payload) - 1
}
size := uint64(end - start)
payload = Payload{
FromClient: false,
Content: DecodeBytes(serverStream.Payload[start:end], format.Format),
Index: start,
Timestamp: serverStream.BlocksTimestamps[serverBlocksIndex],
IsRetransmitted: serverStream.BlocksLoss[serverBlocksIndex],
RegexMatches: findMatchesBetween(serverStream.PatternMatches, serverIndex, serverIndex+size),
}
serverIndex += size
globalIndex += size
serverBlocksIndex++
}
if globalIndex > format.Skip {
payloads = append(payloads, payload)
}
if globalIndex > format.Skip+format.Limit {
return payloads
}
if !hasClientBlocks() {
clientDocumentIndex++
clientBlocksIndex = 0
clientStream = csc.getConnectionStream(c, connectionID, true, clientDocumentIndex)
}
if !hasServerBlocks() {
serverDocumentIndex++
serverBlocksIndex = 0
serverStream = csc.getConnectionStream(c, connectionID, false, serverDocumentIndex)
}
}
return payloads
}
func (csc ConnectionStreamsController) getConnectionStream(c context.Context, connectionID RowID, fromClient bool,
documentIndex int) ConnectionStream {
var result ConnectionStream
if err := csc.storage.Find(ConnectionStreams).Filter(OrderedDocument{
{"connection_id", connectionID},
{"from_client", fromClient},
{"document_index", documentIndex},
}).Context(c).First(&result); err != nil {
log.WithError(err).WithField("connection_id", connectionID).Panic("failed to get a ConnectionStream")
}
return result
}
func findMatchesBetween(patternMatches map[uint][]PatternSlice, from, to uint64) []RegexSlice {
regexSlices := make([]RegexSlice, 0, InitialRegexSlicesCount)
for _, slices := range patternMatches {
for _, slice := range slices {
if from > slice[1] || to <= slice[0] {
continue
}
log.Info(slice[0], slice[1], from, to)
var start, end uint64
if from > slice[0] {
start = 0
} else {
start = slice[0] - from
}
if to <= slice[1] {
end = to - from
} else {
end = slice[1] - from
}
regexSlices = append(regexSlices, RegexSlice{From: start, To: end})
}
}
return regexSlices
}
|