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
//! Streaming response handling for the connection actor.
//!
//! A streamed frame and its protocol terminator inherit the request's
//! correlation identifier, exactly as single, vector, and multi-packet
//! responses do, so a client matching replies by identifier sees one rule for
//! every response shape.
use log::warn;
use super::{ConnectionActor, state::ActorState};
use crate::{
app::Packet,
correlation::CorrelatableFrame,
hooks::ConnectionProtocol,
push::FrameLike,
response::WireframeError,
};
impl<F, E, P> ConnectionActor<F, E, P>
where
F: FrameLike + CorrelatableFrame + Packet,
E: std::fmt::Debug,
P: FrameLike,
{
/// Handle the next frame or error from the streaming response.
pub(super) fn process_response(
&mut self,
res: Option<Result<F, WireframeError<E>>>,
state: &mut ActorState,
out: &mut Vec<F>,
) -> Result<(), WireframeError<E>> {
let is_none = res.is_none();
let produced = self.handle_response(res, state, out)?;
if produced {
self.after_low();
}
if is_none {
self.active_output.clear_response();
}
Ok(())
}
/// Push a frame from the response stream into `out` or handle completion.
///
/// Protocol errors are passed to `handle_error` and do not terminate the
/// actor. I/O errors propagate to the caller.
///
/// Returns `true` if a frame was appended to `out`.
pub(super) fn handle_response(
&mut self,
res: Option<Result<F, WireframeError<E>>>,
state: &mut ActorState,
out: &mut Vec<F>,
) -> Result<bool, WireframeError<E>> {
let mut produced = false;
match res {
Some(Ok(mut frame)) => {
// Response frames inherit the request correlation on every
// path; only the terminator's policy differs below.
self.stamp_response_correlation(&mut frame);
self.process_frame_with_hooks_and_metrics(frame, out);
produced = true;
}
Some(Err(WireframeError::Protocol(e))) => {
warn!("protocol error: error={e:?}");
if let Some(protocol) = self.protocol.as_mut() {
protocol.handle_error(e);
}
state.mark_closed();
// Stop polling the response after a protocol error to avoid
// double-closing and duplicate `on_command_end` signalling.
self.active_output.clear_response();
self.response_failed = true;
self.defer_command_end();
crate::metrics::inc_handler_errors();
}
Some(Err(e)) => return Err(e),
None => {
state.mark_closed();
let terminator = self
.protocol
.as_mut()
.and_then(ConnectionProtocol::stream_end_frame);
if let Some(mut frame) = terminator {
self.terminator_correlation
.apply(&mut frame, self.response_correlation);
self.process_frame_with_hooks_and_metrics(frame, out);
produced = true;
}
// The terminator is still buffered, so the completion hook
// must wait until the sink has written it.
self.defer_command_end();
}
}
Ok(produced)
}
/// Stamp the active response's request correlation onto a frame.
///
/// Every response frame inherits the request's identifier when one was
/// supplied; a frame that already carries its own keeps it when the
/// request has none.
fn stamp_response_correlation(&self, frame: &mut F) {
let Some(correlation_id) = self.response_correlation else {
return;
};
frame.set_correlation_id(Some(correlation_id));
}
}