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
//! Core frame handling context types.
use std::io;
use futures::Sink;
use log::warn;
use crate::{
app::{Envelope, codec_driver::FramePipeline},
codec::FrameCodec,
connection::ConnectionActor,
serializer::Serializer,
};
/// Tracks deserialization failures and enforces a maximum error threshold.
pub(crate) struct DeserFailureTracker<'a> {
/// Counter shared with the connection loop so failures accumulate per peer.
count: &'a mut u32,
/// Threshold at which malformed input terminates the connection.
limit: u32,
}
impl<'a> DeserFailureTracker<'a> {
/// Borrow the loop counter and configure the malformed-input threshold.
pub(crate) const fn new(count: &'a mut u32, limit: u32) -> Self { Self { count, limit } }
/// Record one malformed frame and close the connection at the threshold.
pub(super) fn record(
&mut self,
correlation_id: Option<u64>,
context: &str,
err: impl std::fmt::Debug,
) -> io::Result<()> {
*self.count = (*self.count).saturating_add(1);
warn!("{context}: correlation_id={correlation_id:?}, error={err:?}");
crate::metrics::inc_deser_errors();
if *self.count >= self.limit {
warn!(
"closing connection after {} deserialization failures: {context}",
self.count
);
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"too many deserialization failures",
));
}
Ok(())
}
}
/// Bundles shared dependencies for response forwarding.
pub(crate) struct ResponseContext<'a, S, K, F>
where
S: Serializer + Send + Sync,
K: Sink<F::Frame, Error = io::Error> + Unpin,
F: FrameCodec,
{
/// Serializer used to encode the handler response.
pub(crate) serializer: &'a S,
/// Sink receiving encoded response frames.
pub(crate) sink: &'a mut K,
/// Outbound pipeline that applies fragmentation, `before_send`, and
/// metrics.
pub(crate) pipeline: &'a mut FramePipeline<F::Frame>,
/// Codec that wraps serialized payloads into wire frames.
pub(crate) codec: &'a F,
/// Connection actor that schedules and delivers the response.
///
/// Pushes are already encoded for the wire, so the actor's push frame type
/// is the codec frame rather than the application [`Envelope`].
pub(crate) actor: &'a mut ConnectionActor<Envelope, (), F::Frame>,
}