mod outbound;
use log::warn;
pub(crate) use outbound::{ActorFrameSink, send_native_frame};
#[cfg(test)]
pub(super) use outbound::{OutboundTransport, flush_pipeline_output, send_envelope};
use tokio::io;
use super::{envelope::Envelope, fragmentation_state::FragmentationState};
use crate::{
connection::TerminatorCorrelation,
fragment::{FragmentationConfig, FragmentationError},
hooks::{ConnectionContext, ConnectionProtocol, ProtocolHooks},
};
pub(crate) struct FramePipeline<T = Envelope> {
fragmentation: Option<FragmentationState>,
out: Vec<Envelope>,
protocol: ConnectionProtocol<T, ()>,
terminator_correlation: TerminatorCorrelation,
}
impl<T> FramePipeline<T> {
#[cfg(test)]
pub(crate) fn new(fragmentation: Option<FragmentationConfig>) -> Self {
Self::with_hooks(fragmentation, ProtocolHooks::default())
}
pub(crate) fn with_hooks(
fragmentation: Option<FragmentationConfig>,
hooks: ProtocolHooks<T, ()>,
) -> Self {
Self {
fragmentation: fragmentation.map(FragmentationState::new),
out: Vec::new(),
protocol: ConnectionProtocol::new(hooks),
terminator_correlation: TerminatorCorrelation::PREPARED,
}
}
pub(crate) fn before_send_frame(&mut self, frame: &mut T) { self.protocol.before_send(frame); }
pub(crate) fn on_eof(&mut self, error: &crate::codec::EofError, partial_data: &[u8]) {
self.protocol.on_eof(error, partial_data);
}
pub(crate) fn handle_error(&mut self) { self.protocol.handle_error(()); }
pub(crate) const fn set_context(&mut self, context: ConnectionContext) {
self.protocol.set_context(context);
}
pub(crate) fn stream_end_frame(&mut self) -> Option<T> { self.protocol.stream_end_frame() }
pub(crate) fn on_command_end(&mut self) { self.protocol.on_command_end(); }
pub(crate) const fn terminator_correlation(&self) -> TerminatorCorrelation {
self.terminator_correlation
}
pub(crate) fn process(&mut self, envelope: Envelope) -> io::Result<()> {
let id = envelope.id;
let correlation_id = envelope.correlation_id;
let frames = self.fragment_envelope(envelope).map_err(|err| {
warn!(
"failed to fragment outbound envelope: id={id}, \
correlation_id={correlation_id:?}, error={err:?}"
);
crate::metrics::inc_handler_errors();
io::Error::other(err)
})?;
for frame in frames {
self.push_frame(frame);
}
Ok(())
}
fn fragment_envelope(
&mut self,
envelope: Envelope,
) -> Result<Vec<Envelope>, FragmentationError> {
match self.fragmentation.as_mut() {
Some(state) => state.fragment(envelope),
None => Ok(vec![envelope]),
}
}
pub(crate) fn fragmentation_buffered_bytes(&self) -> usize {
self.fragmentation
.as_ref()
.map_or(0, FragmentationState::buffered_bytes)
}
pub(crate) fn purge_expired(&mut self) {
if let Some(state) = self.fragmentation.as_mut() {
state.purge_expired();
}
}
pub(crate) fn drain_output(&mut self) -> Vec<Envelope> { std::mem::take(&mut self.out) }
pub(crate) const fn fragmentation_mut(&mut self) -> Option<&mut FragmentationState> {
self.fragmentation.as_mut()
}
#[cfg(test)]
pub(crate) const fn has_fragmentation(&self) -> bool { self.fragmentation.is_some() }
fn push_frame(&mut self, envelope: Envelope) {
self.out.push(envelope);
crate::metrics::inc_frames(crate::metrics::Direction::Outbound);
}
}
#[cfg(test)]
#[path = "../codec_driver_tests.rs"]
mod tests;