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
//! Frame processing and emission helpers.
use log::warn;
use super::ConnectionActor;
use crate::{
app::{Packet, fragment_utils::fragment_packet},
correlation::CorrelatableFrame,
push::FrameLike,
};
impl<F, E, P> ConnectionActor<F, E, P>
where
F: FrameLike + CorrelatableFrame + Packet,
E: std::fmt::Debug,
P: FrameLike,
{
/// Emit a multi-packet frame with correlation stamping applied.
pub(super) fn emit_multi_packet_frame(&mut self, mut frame: F, out: &mut Vec<F>) {
self.apply_multi_packet_correlation(&mut frame);
self.process_frame_with_hooks_and_metrics(frame, out);
}
/// Apply correlation stamping to a multi-packet frame.
///
/// Stamping only replaces the frame's identifier when the originating
/// request supplied one. A request without a correlation identifier
/// leaves the producer's own identifier untouched, matching the
/// inheritance rule applied to every other response shape.
pub(super) fn apply_multi_packet_correlation(&mut self, frame: &mut F) {
let Some(ctx) = self.active_output.multi_packet_mut() else {
// No channel is active, so there is nothing to stamp.
return;
};
if !ctx.is_stamping_enabled() {
return;
}
// A request that supplied no correlation identifier must not clear an
// identifier produced by the producer, matching the inheritance rule
// used for single, vector, and streamed responses.
let Some(correlation_id) = ctx.correlation_id() else {
return;
};
// Multi-packet frames inherit the channel's correlation on both
// connection paths: the prepared actor stamps them before its sink
// serializes them, exactly as a directly driven actor does. Only the
// stream terminator's policy differs between the paths, which is what
// `TerminatorCorrelation` names.
frame.set_correlation_id(Some(correlation_id));
debug_assert!(
CorrelatableFrame::correlation_id(frame) == Some(correlation_id)
|| CorrelatableFrame::correlation_id(frame).is_none(),
"multi-packet frame correlation mismatch: expected={correlation_id:?}, got={:?}",
CorrelatableFrame::correlation_id(frame),
);
}
/// Apply fragmentation and protocol hooks before emitting a frame.
///
/// Outbound frame accounting happens in the sink that eventually writes
/// the frame, so this method only composes it.
///
/// # Examples
///
/// ```plaintext
/// actor.process_frame_with_hooks_and_metrics(frame, &mut out);
/// ```
pub(super) fn process_frame_with_hooks_and_metrics(&mut self, frame: F, out: &mut Vec<F>)
where
F: Packet,
{
if let Some(fragmenter) = self.fragmenter.as_ref() {
let fragmented = fragment_packet(fragmenter, frame);
match fragmented {
Ok(frames) => frames
.into_iter()
.for_each(|fragment| self.push_frame(fragment, out)),
Err(err) => {
warn!(
"failed to fragment frame: connection_id={:?}, peer={:?}, error={err:?}",
self.connection_id, self.peer_addr,
);
crate::metrics::inc_handler_errors();
}
}
} else {
self.push_frame(frame, out);
}
}
/// Push a single frame to the current output buffer after applying hooks.
pub(super) fn push_frame(&mut self, mut frame: F, out: &mut Vec<F>) {
if let Some(protocol) = self.protocol.as_mut() {
protocol.before_send(&mut frame);
}
out.push(frame);
}
}