wireframe 0.4.0-alpha1

Simplify building servers and clients for custom binary protocols.
Documentation
//! Reading inbound frames for one connection.
//!
//! The connection loop reads one frame at a time and keeps at most one frame
//! buffered while the previous one is handled. The state a read timeout purges
//! travels with that buffered frame, and every read also observes shutdown:
//! a read timeout only retries the read, so a stop that were not observed here
//! would wait for a peer that keeps its socket open without sending anything.
//! That would pin the connection task, its gauge guard, and the server drain
//! for as long as the peer stayed connected.
//!
//! Two reads are offered. [`InboundFrames::try_next`] is the single
//! non-blocking probe the loop owes the reader after each push batch.
//! [`InboundFrames::next_frame`] reports a read timeout instead of absorbing
//! it, because the streaming loop is what purges expired fragmentation and
//! assembly state and what fails a streaming body whose assembly it evicted.

use std::{io, task::Poll, time::Duration};

use futures::{Stream, StreamExt};
use tokio::time::{Instant, timeout};
use tokio_util::sync::CancellationToken;

use super::super::{codec_driver::FramePipeline, frame_handling};
use crate::message_assembler::{MessageAssemblyState, StreamingAssemblyId};

/// Outcome of waiting for the next inbound frame.
pub(super) enum NextFrame<F> {
    /// A frame is ready for dispatch.
    Ready(F),
    /// The peer closed its input half without sending another frame.
    Closed,
    /// No frame arrived within the configured read timeout.
    TimedOut,
}

/// Poll the buffered frame or the reader, giving up after `bound`.
///
/// A zero bound is a single non-blocking poll rather than a timer: Tokio
/// rounds a timer deadline up to its next tick, so `timeout(Duration::ZERO,
/// ..)` would hold an always-ready competitor — the push lanes — for up to a
/// tick on every pass. Polling once costs no time at all, which is what a
/// caller that owes the push lanes an attempt after each batch needs.
async fn poll_frame<R, F>(
    pending_frame: &mut Option<F>,
    reader: &mut R,
    bound: Duration,
) -> io::Result<NextFrame<F>>
where
    R: Stream<Item = io::Result<F>> + Unpin,
{
    if let Some(frame) = pending_frame.take() {
        return Ok(NextFrame::Ready(frame));
    }
    if bound.is_zero() {
        return Ok(match futures::poll!(reader.next()) {
            Poll::Ready(Some(Ok(frame))) => NextFrame::Ready(frame),
            Poll::Ready(Some(Err(error))) => return Err(error),
            Poll::Ready(None) => NextFrame::Closed,
            Poll::Pending => NextFrame::TimedOut,
        });
    }
    match timeout(bound, reader.next()).await {
        Ok(Some(Ok(frame))) => Ok(NextFrame::Ready(frame)),
        Ok(Some(Err(error))) => Err(error),
        Ok(None) => Ok(NextFrame::Closed),
        Err(_) => Ok(NextFrame::TimedOut),
    }
}

/// Drop expired fragmentation buffers and message assemblies.
///
/// The assemblies the purge evicts are returned to the caller, which is what
/// keeps a recovery pause from discarding the streaming ids whose handlers are
/// still waiting.
pub(super) fn purge_expired<T>(
    pipeline: &mut FramePipeline<T>,
    message_assembly: &mut Option<MessageAssemblyState>,
) -> Vec<StreamingAssemblyId> {
    pipeline.purge_expired();
    let evicted = frame_handling::purge_expired_assemblies(message_assembly);
    evicted.streaming().to_vec()
}

/// Deadline for the current silence, armed on first use.
fn arm_deadline(deadline: &mut Option<Instant>, read_timeout_ms: u64) -> Instant {
    *deadline.get_or_insert_with(|| Instant::now() + Duration::from_millis(read_timeout_ms))
}

/// Start the next idle interval after a purge.
fn restart_deadline(deadline: &mut Option<Instant>, read_timeout_ms: u64) {
    *deadline = Some(Instant::now() + Duration::from_millis(read_timeout_ms));
}

/// Buffered inbound state an outbound write must keep expiring.
///
/// A push write stops running the loop's expiry command while it waits, and it
/// holds the transport's fragmentation pipeline for the whole write, so it
/// borrows the remaining expiry state directly: the reassembled messages, the
/// silence deadline, and the list of streaming bodies the loop must still
/// fail. Sealing a partially written frame is worse than letting this state
/// expire a few frames later, and the pipeline's expired fragment buffers are
/// purged at each frame boundary instead.
pub(super) struct InboundExpiry<'a> {
    /// Reassembled messages the read deadline purges.
    assembly: &'a mut Option<MessageAssemblyState>,
    /// Deadline at which the current silence purges expired state.
    deadline: &'a mut Option<Instant>,
    /// Maximum interval to wait for the next inbound frame.
    read_timeout_ms: u64,
    /// Streaming bodies whose assembly this write evicted.
    ///
    /// A write cannot answer a handler, so the ids are collected for the loop
    /// to fail through its peer-observed path once the batch is written.
    /// Dropping them would leave the handler waiting for chunks that can never
    /// arrive, holding the one streaming slot for the rest of the connection.
    evicted_streams: &'a mut Vec<StreamingAssemblyId>,
}

impl<'a> InboundExpiry<'a> {
    /// Borrow the expiry state of one connection's buffered reads.
    pub(super) const fn new(
        assembly: &'a mut Option<MessageAssemblyState>,
        deadline: &'a mut Option<Instant>,
        read_timeout_ms: u64,
        evicted_streams: &'a mut Vec<StreamingAssemblyId>,
    ) -> Self {
        Self {
            assembly,
            deadline,
            read_timeout_ms,
            evicted_streams,
        }
    }

    /// Instant at which the current silence expires buffered state.
    pub(super) fn due(&mut self) -> Instant { arm_deadline(self.deadline, self.read_timeout_ms) }

    /// Purge expired assemblies once the deadline has passed.
    ///
    /// The deadline is shared with the read path, so a purge here is the same
    /// event that path would have performed; the next silence starts from now
    /// rather than from whenever the write finishes. Evicted streaming bodies
    /// are recorded for the loop rather than failed here, because forwarding
    /// their replies needs the reader this write is holding.
    pub(super) fn expire_silence(&mut self) {
        if arm_deadline(self.deadline, self.read_timeout_ms) <= Instant::now() {
            let purged = super::super::frame_handling::purge_expired_assemblies(self.assembly);
            self.evicted_streams.extend_from_slice(purged.streaming());
            restart_deadline(self.deadline, self.read_timeout_ms);
        }
    }
}

/// Read side of the inbound loop.
///
/// Groups the frame buffered while the previous one was being handled with the
/// state a read timeout purges. They are only ever touched together, and
/// holding them as one value lets the loop read through a single call that also
/// observes shutdown.
pub(super) struct InboundFrames<T, F> {
    /// Frame read while the previous one was still being handled.
    pub(super) pending: Option<T>,
    /// Fragmentation pipeline, whose expired buffers a timeout drops.
    pub(super) pipeline: FramePipeline<F>,
    /// Reassembled messages, likewise purged on a timeout.
    pub(super) message_assembly: Option<MessageAssemblyState>,
    /// Deadline at which the current silence purges expired state.
    ///
    /// Kept here rather than armed inside one wait because a push batch
    /// cancels the read whenever it has frames to deliver. A deadline owned by
    /// the wait would be re-armed from scratch after every batch, so a client
    /// that only ever receives pushes would never reach the purge.
    pub(super) read_deadline: Option<Instant>,
    /// Streaming bodies whose assembly a recovery pause evicted.
    ///
    /// A quarantine pause purges expired assemblies while the connection stays
    /// alive. Their streaming ids cannot be failed from inside the pause,
    /// because failing a body writes its reply, so they wait here for the loop
    /// to act on at the top of its next step — the same place a timed purge is
    /// acted on.
    pub(super) evicted_streams: Vec<StreamingAssemblyId>,
}

impl<T, F> InboundFrames<T, F> {
    /// Poll once for a frame the buffered slot or the reader already holds.
    ///
    /// The connection loop polls pushes and the reader in one biased select, so
    /// an always-ready push lane wins every pass. This gives the reader one
    /// non-blocking chance per pass, which bounds how long an inbound frame can
    /// wait behind a push stream.
    ///
    /// The probe only reads. Expiring buffered state belongs to the loop, which
    /// drives it through [`InboundExpiry::expire_silence`], so a probe cannot
    /// drop state as a side effect of being called. A returned frame still ends
    /// the idle interval, so the next silence gets its full timeout rather than
    /// the rest of this one.
    pub(super) async fn try_next<R>(&mut self, reader: &mut R) -> io::Result<NextFrame<T>>
    where
        R: Stream<Item = io::Result<T>> + Unpin,
    {
        let next = poll_frame(&mut self.pending, reader, Duration::ZERO).await?;
        if !matches!(next, NextFrame::TimedOut) {
            self.read_deadline = None;
        }
        Ok(next)
    }

    /// Read the next frame, reporting a read timeout instead of retrying.
    ///
    /// The streaming loop is what purges expired state and what fails a
    /// streaming body whose assembly it evicted, so a timeout must return
    /// control to that loop rather than being absorbed here. A frame, EOF, or
    /// error still ends the current silence, so the next timeout is measured
    /// from this frame rather than from the previous one.
    ///
    /// The wait is measured against the shared silence deadline rather than a
    /// timer of its own. A push that interrupted the read several seconds into
    /// that silence would otherwise hand back a fresh timeout, so state the
    /// configured interval should have purged would stay buffered.
    pub(super) async fn next_frame<R>(
        &mut self,
        reader: &mut R,
        read_timeout_ms: u64,
        shutdown: &CancellationToken,
    ) -> io::Result<NextFrame<T>>
    where
        R: Stream<Item = io::Result<T>> + Unpin,
    {
        // The shared deadline, not a fresh timer: a push that interrupted an
        // earlier read has already spent part of this silence, and the state it
        // is meant to purge must not get a whole new interval.
        let deadline = arm_deadline(&mut self.read_deadline, read_timeout_ms);
        let remaining = deadline.saturating_duration_since(Instant::now());
        let next = tokio::select! {
            biased;
            () = shutdown.cancelled() => NextFrame::Closed,
            frame = poll_frame(&mut self.pending, reader, remaining) => frame?,
        };
        if matches!(next, NextFrame::TimedOut) {
            // The interval is spent, so the next silence starts now rather than
            // leaving an expired deadline that every later read would trip.
            restart_deadline(&mut self.read_deadline, read_timeout_ms);
        } else {
            self.read_deadline = None;
        }
        Ok(next)
    }
}

/// Buffered read state holding one incomplete assembly that any purge evicts.
///
/// The assembly timeout is zero so that a purge always drops it; assertions
/// then measure *when* the purge runs rather than the assembly clock, which is
/// a `std::time::Instant` that a paused runtime does not advance.
#[cfg(test)]
pub(super) fn incomplete_inbound<T>() -> InboundFrames<T, T> {
    use crate::message_assembler::{
        EnvelopeRouting,
        FirstFrameHeader,
        FirstFrameInput,
        MessageAssemblyState,
        MessageKey,
    };

    let mut assembly = MessageAssemblyState::new(
        std::num::NonZeroUsize::new(1024).unwrap_or(std::num::NonZeroUsize::MIN),
        Duration::ZERO,
    );
    let first = FirstFrameHeader {
        message_key: MessageKey(1),
        metadata_len: 0,
        body_len: 4,
        total_body_len: Some(8),
        is_last: false,
    };
    let input = FirstFrameInput::new(&first, EnvelopeRouting::default(), Vec::new(), b"half")
        .expect("the incomplete frame is valid");
    assembly
        .accept_first_frame(input)
        .expect("the first frame is buffered");
    InboundFrames {
        pending: None,
        pipeline: FramePipeline::new(None),
        message_assembly: Some(assembly),
        read_deadline: None,
        evicted_streams: Vec::new(),
    }
}

/// Number of incomplete assemblies still buffered.
#[cfg(test)]
pub(super) fn buffered_assemblies<T, F>(inbound: &InboundFrames<T, F>) -> Option<usize> {
    inbound
        .message_assembly
        .as_ref()
        .map(crate::message_assembler::MessageAssemblyState::buffered_count)
}

#[cfg(test)]
#[path = "frames_tests.rs"]
mod tests;