Lines
97.73 %
Functions
100 %
Branches
//! The reader task of an eval channel.
//!
//! It never stops listening: while a request waits for room in its stream's
//! queue, new chunks are still decoded, an `(:interrupt n)` reaches the mux at
//! once. Requests and `(:close n)` frames wait in a short FIFO, so their order
//! holds. A framing error stops the input; the frames already taken are still
//! answered before the `parse` reply. A client close aborts through the mux's [`rpc::mux::AbortHandle`].
use std::collections::VecDeque;
use rpc::envelope::{Frame, read_frame};
use rpc::mux::{Mux, QUEUE_CAPACITY, reject_input};
use rpc::{FrameDecoder, FrameError};
use tokio::sync::mpsc;
pub(super) async fn read_input(mut mux: Mux, mut chunks: mpsc::Receiver<Vec<u8>>) {
let replies = mux.replies();
let mut decoder = FrameDecoder::new();
let mut held: VecDeque<String> = VecDeque::new();
let mut input_open = true;
let mut failed = None;
while input_open || !held.is_empty() {
tokio::select! {
biased;
() = ready_front(&mux, held.front()), if !held.is_empty() => {
let Some(frame) = held.pop_front() else { continue };
if let Some(refusal) = mux.frame(&frame) {
replies.send(refusal).await.ok();
}
chunk = chunks.recv(), if input_open && held.len() < QUEUE_CAPACITY => {
let Some(bytes) = chunk else {
input_open = false;
continue;
};
if let Err(err) = take_chunk(&mut decoder, &mut mux, &mut held, &bytes) {
failed = Some(err);
if let Some(err) = failed {
replies.send(reject_input(&err)).await.ok();
async fn ready_front(mux: &Mux, front: Option<&String>) {
if let Some(frame) = front {
mux.ready(frame).await;
/// Interrupts go to the mux now; everything else waits its turn.
fn take_chunk(
decoder: &mut FrameDecoder,
mux: &mut Mux,
held: &mut VecDeque<String>,
bytes: &[u8],
) -> Result<(), FrameError> {
decoder.feed(bytes)?;
while let Some(frame) = decoder.next_frame() {
let frame = frame?;
match read_frame(&frame) {
Ok(Frame::Interrupt(stream)) => mux.interrupt(stream),
_ => held.push_back(frame),
Ok(())