-
Notifications
You must be signed in to change notification settings - Fork 0
/
messages.go
299 lines (250 loc) · 7.37 KB
/
messages.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/protobuf/proto"
"github.com/lasthyphen/dijetsnodesgo/ids"
"github.com/lasthyphen/dijetsnodesgo/utils/compression"
"github.com/lasthyphen/dijetsnodesgo/utils/constants"
"github.com/lasthyphen/dijetsnodesgo/utils/math"
"github.com/lasthyphen/dijetsnodesgo/utils/metric"
"github.com/lasthyphen/dijetsnodesgo/utils/timer/mockable"
"github.com/lasthyphen/dijetsnodesgo/utils/wrappers"
p2ppb "github.com/lasthyphen/dijetsnodesgo/proto/pb/p2p"
)
var (
_ InboundMessage = (*inboundMessage)(nil)
_ OutboundMessage = (*outboundMessage)(nil)
)
// InboundMessage represents a set of fields for an inbound message
type InboundMessage interface {
// NodeID returns the ID of the node that sent this message
NodeID() ids.NodeID
// Op returns the op that describes this message type
Op() Op
// Message returns the message that was sent
Message() any
// Expiration returns the time that the sender will have already timed out
// this request
Expiration() time.Time
// OnFinishedHandling must be called one time when this message has been
// handled by the message handler
OnFinishedHandling()
// BytesSavedCompression returns the number of bytes that this message saved
// due to being compressed
BytesSavedCompression() int
}
type inboundMessage struct {
nodeID ids.NodeID
op Op
message any
expiration time.Time
onFinishedHandling func()
bytesSavedCompression int
}
func (m *inboundMessage) NodeID() ids.NodeID {
return m.nodeID
}
func (m *inboundMessage) Op() Op {
return m.op
}
func (m *inboundMessage) Message() any {
return m.message
}
func (m *inboundMessage) Expiration() time.Time {
return m.expiration
}
func (m *inboundMessage) OnFinishedHandling() {
if m.onFinishedHandling != nil {
m.onFinishedHandling()
}
}
func (m *inboundMessage) BytesSavedCompression() int {
return m.bytesSavedCompression
}
// OutboundMessage represents a set of fields for an outbound message that can
// be serialized into a byte stream
type OutboundMessage interface {
// BypassThrottling returns true if we should send this message, regardless
// of any outbound message throttling
BypassThrottling() bool
// Op returns the op that describes this message type
Op() Op
// Bytes returns the bytes that will be sent
Bytes() []byte
// BytesSavedCompression returns the number of bytes that this message saved
// due to being compressed
BytesSavedCompression() int
}
type outboundMessage struct {
bypassThrottling bool
op Op
bytes []byte
bytesSavedCompression int
}
func (m *outboundMessage) BypassThrottling() bool {
return m.bypassThrottling
}
func (m *outboundMessage) Op() Op {
return m.op
}
func (m *outboundMessage) Bytes() []byte {
return m.bytes
}
func (m *outboundMessage) BytesSavedCompression() int {
return m.bytesSavedCompression
}
// TODO: add other compression algorithms with extended interface
type msgBuilder struct {
gzipCompressor compression.Compressor
compressTimeMetrics map[Op]metric.Averager
decompressTimeMetrics map[Op]metric.Averager
maxMessageTimeout time.Duration
}
func newMsgBuilder(
namespace string,
metrics prometheus.Registerer,
maxMessageTimeout time.Duration,
) (*msgBuilder, error) {
cpr, err := compression.NewGzipCompressor(constants.DefaultMaxMessageSize)
if err != nil {
return nil, err
}
mb := &msgBuilder{
gzipCompressor: cpr,
compressTimeMetrics: make(map[Op]metric.Averager, len(ExternalOps)),
decompressTimeMetrics: make(map[Op]metric.Averager, len(ExternalOps)),
maxMessageTimeout: maxMessageTimeout,
}
errs := wrappers.Errs{}
for _, op := range ExternalOps {
mb.compressTimeMetrics[op] = metric.NewAveragerWithErrs(
namespace,
fmt.Sprintf("%s_compress_time", op),
fmt.Sprintf("time (in ns) to compress %s messages", op),
metrics,
&errs,
)
mb.decompressTimeMetrics[op] = metric.NewAveragerWithErrs(
namespace,
fmt.Sprintf("%s_decompress_time", op),
fmt.Sprintf("time (in ns) to decompress %s messages", op),
metrics,
&errs,
)
}
return mb, errs.Err
}
func (mb *msgBuilder) marshal(
uncompressedMsg *p2ppb.Message,
gzipCompress bool,
) ([]byte, int, time.Duration, error) {
uncompressedMsgBytes, err := proto.Marshal(uncompressedMsg)
if err != nil {
return nil, 0, 0, err
}
if !gzipCompress {
return uncompressedMsgBytes, 0, 0, nil
}
// If compression is enabled, we marshal twice:
// 1. the original message
// 2. the message with compressed bytes
//
// This recursive packing allows us to avoid an extra compression on/off
// field in the message.
startTime := time.Now()
compressedBytes, err := mb.gzipCompressor.Compress(uncompressedMsgBytes)
if err != nil {
return nil, 0, 0, err
}
compressedMsg := p2ppb.Message{
Message: &p2ppb.Message_CompressedGzip{
CompressedGzip: compressedBytes,
},
}
compressedMsgBytes, err := proto.Marshal(&compressedMsg)
if err != nil {
return nil, 0, 0, err
}
compressTook := time.Since(startTime)
bytesSaved := len(uncompressedMsgBytes) - len(compressedMsgBytes)
return compressedMsgBytes, bytesSaved, compressTook, nil
}
func (mb *msgBuilder) unmarshal(b []byte) (*p2ppb.Message, bool, int, time.Duration, error) {
m := new(p2ppb.Message)
if err := proto.Unmarshal(b, m); err != nil {
return nil, false, 0, 0, err
}
compressed := m.GetCompressedGzip()
if len(compressed) == 0 {
// The message wasn't compressed
return m, false, 0, 0, nil
}
startTime := time.Now()
decompressed, err := mb.gzipCompressor.Decompress(compressed)
if err != nil {
return nil, true, 0, 0, err
}
if err := proto.Unmarshal(decompressed, m); err != nil {
return nil, true, 0, 0, err
}
decompressTook := time.Since(startTime)
bytesSavedCompression := len(decompressed) - len(compressed)
return m, true, bytesSavedCompression, decompressTook, nil
}
func (mb *msgBuilder) createOutbound(m *p2ppb.Message, gzipCompress bool, bypassThrottling bool) (*outboundMessage, error) {
b, saved, compressTook, err := mb.marshal(m, gzipCompress)
if err != nil {
return nil, err
}
op, err := ToOp(m)
if err != nil {
return nil, err
}
if gzipCompress {
mb.compressTimeMetrics[op].Observe(float64(compressTook))
}
return &outboundMessage{
bypassThrottling: bypassThrottling,
op: op,
bytes: b,
bytesSavedCompression: saved,
}, nil
}
func (mb *msgBuilder) parseInbound(
bytes []byte,
nodeID ids.NodeID,
onFinishedHandling func(),
) (*inboundMessage, error) {
m, wasCompressed, bytesSavedCompression, decompressTook, err := mb.unmarshal(bytes)
if err != nil {
return nil, err
}
op, err := ToOp(m)
if err != nil {
return nil, err
}
msg, err := Unwrap(m)
if err != nil {
return nil, err
}
if wasCompressed {
mb.decompressTimeMetrics[op].Observe(float64(decompressTook))
}
expiration := mockable.MaxTime
if deadline, ok := GetDeadline(msg); ok {
deadline = math.Min(deadline, mb.maxMessageTimeout)
expiration = time.Now().Add(deadline)
}
return &inboundMessage{
nodeID: nodeID,
op: op,
message: msg,
expiration: expiration,
onFinishedHandling: onFinishedHandling,
bytesSavedCompression: bytesSavedCompression,
}, nil
}