use std::sync::Arc;
use super::{
ConnectionActor,
FrameSink,
PushSink,
drain::ActorStep,
drive::ResponseOutcome,
event::{Event, EventWatch},
state::ActorState,
writes::{self, WriteOutcome},
};
use crate::{
app::Packet,
correlation::CorrelatableFrame,
metrics::ResponseCancellationReason,
push::FrameLike,
response::WireframeError,
};
struct PendingOutput<F, P> {
frames: Vec<F>,
pushes: Vec<P>,
}
impl<F, P> PendingOutput<F, P> {
const fn new() -> Self {
Self {
frames: Vec::new(),
pushes: Vec::new(),
}
}
}
impl<F, E, P> ConnectionActor<F, E, P>
where
F: FrameLike + CorrelatableFrame + Packet,
E: std::fmt::Debug,
P: FrameLike,
{
pub(super) async fn drive_bounded_drain<S>(
&mut self,
sink: &mut S,
) -> Result<ResponseOutcome, WireframeError<E>>
where
S: FrameSink<F> + PushSink<P> + Send,
{
let mut push_lanes_discarded = false;
while self.active_output.is_response() || self.active_output.is_multi_packet() {
let mut state = ActorState::new(
self.active_output.is_response(),
self.active_output.is_multi_packet(),
);
let timer = Arc::clone(&self.grace_timer);
let mut grace = timer.elapse(self.peer_closed_grace);
let watch_shutdown = self.abandon_response_on_shutdown || !push_lanes_discarded;
let event = tokio::select! {
biased;
event = self.next_event_in_bounded_drain(&state, watch_shutdown) => event,
() = grace.as_mut() => {
self.cancel_stalled_response();
return Ok(ResponseOutcome::Cancelled);
}
};
let shutdown_observed = matches!(event, Event::Shutdown);
if shutdown_observed && self.abandon_response_on_shutdown {
ResponseCancellationReason::Shutdown.record();
self.discard_push_lanes();
return Ok(ResponseOutcome::Cancelled);
}
if shutdown_observed {
self.discard_push_lanes();
push_lanes_discarded = true;
continue;
}
let mut output = PendingOutput::new();
let mut step = ActorStep {
state: &mut state,
out: &mut output.frames,
pushes: &mut output.pushes,
};
self.dispatch_event(event, &mut step)?;
if self.write_after_peer_close(output, sink).await? == WriteOutcome::Cancelled {
return Ok(ResponseOutcome::Cancelled);
}
self.run_deferred_command_end();
}
Ok(self.finish_outcome())
}
pub(super) async fn next_event_while_draining(&mut self, state: &ActorState) -> Event<F, E, P> {
let watch = EventWatch {
peer: true,
shutdown: true,
};
self.next_event_inner(state, watch).await
}
pub(super) async fn next_event_in_bounded_drain(
&mut self,
state: &ActorState,
watch_shutdown: bool,
) -> Event<F, E, P> {
let watch = EventWatch {
peer: false,
shutdown: watch_shutdown,
};
self.next_event_inner(state, watch).await
}
async fn write_after_peer_close<S>(
&mut self,
output: PendingOutput<F, P>,
sink: &mut S,
) -> Result<WriteOutcome, WireframeError<E>>
where
S: FrameSink<F> + PushSink<P> + Send,
{
let PendingOutput { frames, pushes } = output;
let progress = sink.progress();
let lanes = pushes
.into_iter()
.map(writes::Output::Push)
.chain(frames.into_iter().map(writes::Output::Frame));
for item in lanes {
let outcome = self
.write_output_after_peer_close(progress.clone(), item, sink)
.await?;
if outcome == WriteOutcome::Cancelled {
return Ok(WriteOutcome::Cancelled);
}
}
Ok(WriteOutcome::Written)
}
async fn write_output_after_peer_close<S>(
&mut self,
progress: Option<Arc<super::ProgressSignal>>,
item: writes::Output<F, P>,
sink: &mut S,
) -> Result<WriteOutcome, WireframeError<E>>
where
S: FrameSink<F> + PushSink<P> + Send,
{
let limit = {
let bounds = self.write_bounds();
match item {
writes::Output::Push(push) => {
let mut pinned = std::pin::pin!(sink.send_push(push));
writes::write_within_grace(pinned.as_mut(), &bounds, progress.as_deref()).await
}
writes::Output::Frame(frame) => {
let mut pinned = std::pin::pin!(sink.send_frame(frame));
writes::write_within_grace(pinned.as_mut(), &bounds, progress.as_deref()).await
}
}
.map_err(WireframeError::Io)?
};
Ok(self.record_write_limit(limit))
}
}