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
//! Decoding one inbound frame's payload into an [`Envelope`].
//!
//! The frame loop hands each raw frame to
//! [`core::decode_and_assemble`](super::core::decode_and_assemble), which decodes its
//! envelope here, then reassembles and assembles it. Decoding is separated
//! from those later stages because it is the only one that depends on the
//! serializer's metadata layout rather than on connection-local buffering.
use super::super::envelope::Envelope;
use crate::{
frame::FrameMetadata,
message::{DecodeWith, DeserializeContext},
serializer::Serializer,
};
/// Benchmark entry point for the inbound decode seam.
///
/// Mirrors the crate-internal envelope decoder for harnesses that live outside
/// the crate. It exists only with the `test-support` feature and returns the
/// decoded envelope rather than any internal state, so a caller cannot depend
/// on how the decode is staged.
///
/// # Errors
///
/// Returns the decoder's error when `payload` is not a complete envelope, or
/// when trailing bytes follow it.
///
/// # Examples
///
/// ```ignore
/// let (envelope, consumed) = benchmark_decode_envelope(&BincodeSerializer, &frame_payload)?;
/// assert_eq!(envelope.payload_bytes().len(), payload_len);
/// ```
#[cfg(feature = "test-support")]
#[doc(hidden)]
pub fn benchmark_decode_envelope<S>(
serializer: &S,
payload: &[u8],
) -> std::result::Result<(Envelope, usize), Box<dyn std::error::Error + Send + Sync>>
where
S: Serializer + FrameMetadata<Frame = Envelope> + Send + Sync,
Envelope: DecodeWith<S>,
{
parse_envelope(serializer, payload)
}
/// Decode exactly one envelope, using metadata to select full deserialization.
pub(super) fn parse_envelope<S>(
serializer: &S,
payload: &[u8],
) -> std::result::Result<(Envelope, usize), Box<dyn std::error::Error + Send + Sync>>
where
S: Serializer + FrameMetadata<Frame = Envelope> + Send + Sync,
Envelope: DecodeWith<S>,
{
let (envelope, consumed) = match serializer.parse(payload) {
Ok((parsed_envelope, metadata_bytes_consumed)) => {
if serializer.should_deserialize_after_parse() {
let context = DeserializeContext {
frame_metadata: payload.get(..metadata_bytes_consumed),
message_id: Some(parsed_envelope.id),
correlation_id: parsed_envelope.correlation_id,
metadata_bytes_consumed: Some(metadata_bytes_consumed),
};
serializer.deserialize_with_context::<Envelope>(payload, &context)
} else {
Ok((parsed_envelope, metadata_bytes_consumed))
}
}
Err(error) if is_bincode_limit_exceeded(&error) => {
Err(Box::new(error) as Box<dyn std::error::Error + Send + Sync>)
}
Err(_) => serializer.deserialize::<Envelope>(payload),
}?;
if consumed != payload.len() {
crate::metrics::inc_decode_rejection(
crate::metrics::DecodeRejectionPath::Envelope,
crate::metrics::DecodeRejectionReason::TrailingBytes,
);
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"trailing bytes after envelope: frame must contain exactly one value",
)
.into());
}
// The envelope owns a freshly allocated payload vector, which is the
// inbound copy. Recording from the decoded payload's length measures the
// bytes the message holds, not the frame's metadata, and it sits after the
// trailing-bytes check so a rejected frame is not counted as decoded.
crate::benchmark_counters::record_deserialized(envelope.payload_bytes().len());
Ok((envelope, consumed))
}
/// Detect a bincode budget failure so fallback decoding does not repeat the work.
fn is_bincode_limit_exceeded(error: &(dyn std::error::Error + 'static)) -> bool {
error
.downcast_ref::<bincode::error::DecodeError>()
.is_some_and(|decode_error| {
matches!(decode_error, bincode::error::DecodeError::LimitExceeded)
})
}