use std::collections::HashMap;
use futures_util::{SinkExt, StreamExt};
use tokio::sync::{broadcast, mpsc, oneshot};
use shep_core::protocol::{
BusEvent, Envelope, Reply, Request, Response, ServerFrame, decode_frame, encode_frame, reply_id,
};
use crate::client::RequestError;
use crate::connection::Frames;
pub(crate) const EVENT_CHANNEL_CAPACITY: usize = 64;
const COMMAND_CHANNEL_CAPACITY: usize = 64;
pub(crate) enum Command {
Request {
body: Request,
deadline_ms: Option<u64>,
reply_to: oneshot::Sender<Result<Response, RequestError>>,
},
Subscribe {
reply_to: oneshot::Sender<broadcast::Receiver<BusEvent>>,
},
}
pub(crate) fn spawn(frames: Frames) -> mpsc::Sender<Command> {
let (commands_tx, commands_rx) = mpsc::channel(COMMAND_CHANNEL_CAPACITY);
let (events_tx, _events_rx) = broadcast::channel(EVENT_CHANNEL_CAPACITY);
tokio::spawn(run(frames, commands_rx, events_tx));
commands_tx
}
async fn run(
mut frames: Frames,
mut commands: mpsc::Receiver<Command>,
events: broadcast::Sender<BusEvent>,
) {
let mut next_id: u64 = 1;
let mut pending: HashMap<u64, oneshot::Sender<Result<Response, RequestError>>> = HashMap::new();
loop {
tokio::select! {
command = commands.recv() => {
match command {
Some(Command::Request { body, deadline_ms, reply_to }) => {
let alive = send_request(&mut frames, &mut next_id, &mut pending, body, deadline_ms, reply_to).await;
pending.retain(|_, tx| !tx.is_closed());
if !alive {
break; }
}
Some(Command::Subscribe { reply_to }) => {
let _ = reply_to.send(events.subscribe());
}
None => break, }
}
frame = frames.next() => {
match frame {
Some(Ok(bytes)) => route_frame(&bytes, &mut pending, &events),
Some(Err(_)) | None => break, }
}
}
}
for (_id, reply_to) in pending.drain() {
let _ = reply_to.send(Err(RequestError::Closed)); }
}
async fn send_request(
frames: &mut Frames,
next_id: &mut u64,
pending: &mut HashMap<u64, oneshot::Sender<Result<Response, RequestError>>>,
body: Request,
deadline_ms: Option<u64>,
reply_to: oneshot::Sender<Result<Response, RequestError>>,
) -> bool {
let id = *next_id;
*next_id += 1;
let envelope = Envelope {
id,
deadline_ms,
body,
};
let payload = match encode_frame(&envelope) {
Ok(payload) => payload,
Err(err) => {
let _ = reply_to.send(Err(RequestError::Wire(err)));
return true; }
};
if frames.send(payload).await.is_err() {
let _ = reply_to.send(Err(RequestError::Closed));
return false;
}
pending.insert(id, reply_to);
true
}
fn route_frame(
bytes: &[u8],
pending: &mut HashMap<u64, oneshot::Sender<Result<Response, RequestError>>>,
events: &broadcast::Sender<BusEvent>,
) {
let frame = match decode_frame::<ServerFrame>(bytes) {
Ok(frame) => frame,
Err(err) => {
if let Some(id) = reply_id(bytes)
&& let Some(reply_to) = pending.remove(&id)
{
let _ = reply_to.send(Err(RequestError::Undecodable(err)));
}
return;
}
};
match frame {
ServerFrame::Reply(Reply { id, result }) => {
if let Some(reply_to) = pending.remove(&id) {
let _ = reply_to.send(result.map_err(RequestError::Rpc));
}
}
ServerFrame::Event(event) => {
let _ = events.send(event); }
_ => {}
}
}
#[cfg(test)]
mod tests {
use futures_util::FutureExt;
use shep_core::protocol::{ProcessEventKind, ProcessInfo, codec};
use shep_core::status::ProcStatus;
use tokio_util::codec::Framed;
use super::*;
use crate::client::RequestError;
use crate::connection::ByteStream;
#[tokio::test]
async fn the_actor_answers_a_request_over_an_in_memory_stream() {
let (near, far) = tokio::io::duplex(1024);
let frames = Framed::new(Box::new(near) as Box<dyn ByteStream>, codec());
let commands = spawn(frames);
let mut far = Framed::new(far, codec());
let (reply_to, answer) = oneshot::channel();
commands
.send(Command::Request {
body: Request::Ping,
deadline_ms: None,
reply_to,
})
.await
.unwrap();
let sent: Envelope = decode_frame(&far.next().await.unwrap().unwrap()).unwrap();
let reply = ServerFrame::Reply(Reply {
id: sent.id,
result: Ok(Response::Pong),
});
far.send(encode_frame(&reply).unwrap()).await.unwrap();
assert!(matches!(answer.await.unwrap(), Ok(Response::Pong)));
let (reply_to, answer) = oneshot::channel();
commands
.send(Command::Request {
body: Request::Ping,
deadline_ms: None,
reply_to,
})
.await
.unwrap();
let _second = far.next().await.unwrap().unwrap();
drop(far);
assert!(
matches!(answer.await.unwrap(), Err(RequestError::Closed)),
"a request pending when the stream closes must resolve as closed"
);
}
#[test]
fn an_undecodable_reply_fails_its_caller_by_id_rather_than_hanging() {
let (tx, rx) = oneshot::channel();
let mut pending = HashMap::from([(7_u64, tx)]);
let (events, _) = broadcast::channel(4);
route_frame(
br#"{"id":7,"result":{"Ok":{"kind":"from_the_future","data":{"a":1}}}}"#,
&mut pending,
&events,
);
let got = rx.now_or_never().expect("the caller must be answered now");
assert!(matches!(got, Ok(Err(RequestError::Undecodable(_)))));
assert!(pending.is_empty(), "the pending entry must be cleared");
}
#[test]
fn a_frame_of_a_kind_this_build_does_not_know_is_dropped_without_disturbing_the_connection() {
let (tx, rx) = oneshot::channel();
let mut pending = HashMap::from([(7_u64, tx)]);
let (events, mut sub) = broadcast::channel(4);
route_frame(br#"{"progress":{"done":1}}"#, &mut pending, &events);
assert!(sub.try_recv().is_err(), "nothing decodable to deliver");
assert_eq!(pending.len(), 1, "the pending request is still held");
assert!(rx.now_or_never().is_none(), "its caller is not answered");
}
#[test]
fn a_frame_with_one_half_of_the_reply_pair_is_dropped_too() {
let (events, mut sub) = broadcast::channel(4);
for frame in [
r#"{"id":7,"kind":"progress","percent":40}"#,
r#"{"result":{"done":1}}"#,
] {
let (tx, rx) = oneshot::channel();
let mut pending = HashMap::from([(7_u64, tx)]);
route_frame(frame.as_bytes(), &mut pending, &events);
assert!(sub.try_recv().is_err(), "{frame} delivered nothing");
assert_eq!(pending.len(), 1, "{frame} left the request held");
assert!(rx.now_or_never().is_none(), "{frame} answered no caller");
}
}
#[test]
fn an_undecodable_event_is_dropped_without_disturbing_the_connection() {
let mut pending = HashMap::new();
let (events, mut sub) = broadcast::channel(4);
route_frame(
br#"{"event":"from_the_future","data":{"a":1}}"#,
&mut pending,
&events,
);
assert!(sub.try_recv().is_err(), "nothing decodable to deliver");
}
#[test]
fn an_event_carrying_a_status_this_build_does_not_know_is_delivered() {
let mut pending = HashMap::new();
let (events, mut sub) = broadcast::channel(4);
let event = BusEvent::Process {
event: ProcessEventKind::Online,
info: ProcessInfo::builder(3, "web", ProcStatus::Online).build(),
manually: false,
at_ms: 0,
};
let mut wire = serde_json::to_value(&event).unwrap();
wire["data"]["info"]["status"] = serde_json::json!("invented_next_year");
route_frame(wire.to_string().as_bytes(), &mut pending, &events);
let BusEvent::Process { info, .. } = sub.try_recv().expect("the event is delivered") else {
panic!("a process event");
};
assert_eq!(info.status, ProcStatus::Unrecognized);
}
}