use std::{io, time::Duration};
use bytes::Bytes;
use futures::Sink;
use super::{
super::{
envelope::{Envelope, Packet},
frame_handling::{self, Dispatch, MemoryPressureAction},
},
stream_active::PendingEvent,
stream_dispatch::Step,
stream_handles::{ChunkBudget, DispatchHandles, LoopState},
stream_parked::{dispatch_parked, parked_bytes},
};
use crate::{
codec::FrameCodec,
message::EncodeWith,
message_assembler::StreamingAssemblyId,
serializer::Serializer,
};
pub(super) const fn finish_step(dispatch: Dispatch) -> Step {
match dispatch {
Dispatch::Continue => Step::Continue,
Dispatch::Stop => Step::Closed,
}
}
pub(super) enum AdmissionStep {
Continue,
Handler(Option<Bytes>),
}
pub(super) async fn drive_pending_admission(
state: &mut LoopState,
wait: Duration,
budget: ChunkBudget,
) -> AdmissionStep {
#[cfg(feature = "test-support")]
observe_retained(state, budget);
match state.streams.drive_pending(wait, budget).await {
PendingEvent::Send(result) => state.streams.complete_send(result),
PendingEvent::Handler(outcome) => return AdmissionStep::Handler(outcome),
PendingEvent::Idle => return AdmissionStep::Continue,
}
AdmissionStep::Continue
}
#[cfg(feature = "test-support")]
fn observe_retained(state: &LoopState, budget: ChunkBudget) {
let streams = &state.streams;
let retained = streams
.retained_bytes()
.saturating_add(budget.other_retained)
.saturating_add(streams.pending_chunk_len());
crate::test_helpers::retained_bytes::record(budget.limit, retained);
}
pub(super) fn expire_finished_assemblies<S, E, K, F>(
handles: &mut DispatchHandles<'_, S, E, K, F>,
) -> Vec<StreamingAssemblyId>
where
S: Serializer + Send + Sync,
E: Packet,
F: FrameCodec,
K: Sink<F::Frame, Error = io::Error> + Unpin + Send,
{
let purged = frame_handling::purge_expired_assemblies(handles.assembly);
handles.pipeline.purge_expired();
purged.streaming().to_vec()
}
pub(super) async fn apply_memory_pressure<S, E, K, F>(
state: &mut LoopState,
handles: &mut DispatchHandles<'_, S, E, K, F>,
) -> io::Result<()>
where
S: Serializer + Send + Sync,
E: Packet,
F: FrameCodec,
K: Sink<F::Frame, Error = io::Error> + Unpin + Send,
{
let pressure = frame_handling::evaluate_memory_pressure_with_retained(
handles.assembly.as_ref(),
Some(handles.ctx.effective_budgets),
handles.pipeline.fragmentation_buffered_bytes(),
state
.streams
.retained_bytes()
.saturating_add(parked_bytes(&state.parked)),
);
if matches!(pressure, MemoryPressureAction::Abort) {
state.streams.abort();
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"per-connection memory budget hard cap exceeded",
));
}
frame_handling::apply_memory_pressure(pressure, || {}).await
}
pub(super) async fn drain_parked<S, E, K, F>(
state: &mut LoopState,
handles: &mut DispatchHandles<'_, S, E, K, F>,
) -> io::Result<Dispatch>
where
S: Serializer + Send + Sync,
E: Packet,
F: FrameCodec,
K: Sink<F::Frame, Error = io::Error> + Unpin + Send,
Envelope: EncodeWith<S>,
{
let mut dispatch = Dispatch::Continue;
while let Some(message) = state.pop_parked() {
dispatch = dispatch_parked(message, &state.parked, handles.reborrow()).await?;
if dispatch == Dispatch::Stop {
break;
}
}
Ok(dispatch)
}
pub(super) async fn fail_body_ending_with<S, E, K, F>(
state: &mut LoopState,
handles: &mut DispatchHandles<'_, S, E, K, F>,
reason: io::Error,
) -> io::Result<Dispatch>
where
S: Serializer + Send + Sync,
E: Packet,
F: FrameCodec,
K: Sink<F::Frame, Error = io::Error> + Unpin + Send,
Envelope: EncodeWith<S>,
{
let budget = ChunkBudget::measure(handles, &state.parked);
state
.streams
.close_active(Some(reason), budget, handles.reborrow())
.await
}