Pass errors from deser down to the recievers

This commit is contained in:
QuantumEntangledAndy authored and Andrew King committed 2023-02-28 12:41:04 +07:00
1 parent e7b9aee122
commit fa155df974
2 files changed
+63 -44

No files matched your search

@@ -15,7 +15,7 @@ type MsgHandler = Box<dyn Fn(&Bc) -> Option<Bc> + Send + Sync>;
#[derive(Default)]
struct Subscriber {
/// Subscribers based on their Num
num: RwLock<BTreeMap<u16, Sender<Bc>>>,
num: RwLock<BTreeMap<u16, Sender<Result<Bc>>>>,
/// Subscribers based on their ID
id: RwLock<BTreeMap<u32, MsgHandler>>,
}
@@ -48,11 +48,7 @@ impl BcConnection {
loop {
let packet = source.next().await;
let bc = match packet {
Some(Ok(bc)) => bc,
Some(Err(e)) => {
error!("Deserialization error: {:?}", e);
break;
}
Some(res) => res,
None => continue,
};
@@ -119,39 +115,55 @@ impl BcConnection {
Ok(())
}
async fn poll(response: Bc, subscribers: &Subscriber, sink: &Mutex<BcConnSink>) -> Result<()> {
// Don't hold the lock during deserialization so we don't poison the subscribers mutex if
// something goes wrong
let msg_num = response.meta.msg_num;
let msg_id = response.meta.msg_id;
async fn poll(
response: Result<Bc>,
subscribers: &Subscriber,
sink: &Mutex<BcConnSink>,
) -> Result<()> {
match response {
Ok(response) => {
// Don't hold the lock during deserialization so we don't poison the subscribers mutex if
// something goes wrong
let msg_num = response.meta.msg_num;
let msg_id = response.meta.msg_id;
let mut remove_it_num = false;
match (
subscribers.id.read().await.get(&msg_id),
subscribers.num.read().await.get(&msg_num),
) {
(Some(occ), _) => {
if let Some(reply) = occ(&response) {
assert!(reply.meta.msg_num == response.meta.msg_num);
sink.lock().await.send(reply).await?;
let mut remove_it_num = false;
match (
subscribers.id.read().await.get(&msg_id),
subscribers.num.read().await.get(&msg_num),
) {
(Some(occ), _) => {
if let Some(reply) = occ(&response) {
assert!(reply.meta.msg_num == response.meta.msg_num);
sink.lock().await.send(reply).await?;
}
}
(None, Some(occ)) => {
trace!("occ.full: {}/{}", occ.capacity(), occ.max_capacity());
if occ.send(Ok(response)).await.is_err() {
remove_it_num = true;
}
}
(None, None) => {
debug!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
);
trace!("Contents: {:?}", response);
}
}
if remove_it_num {
subscribers.num.write().await.remove(&msg_num);
}
}
(None, Some(occ)) => {
trace!("occ.full: {}/{}", occ.capacity(), occ.max_capacity());
if occ.send(response).await.is_err() {
remove_it_num = true;
Err(e) => {
for sub in subscribers.num.read().await.values() {
let _ = sub.send(Err(e.clone())).await;
}
subscribers.num.write().await.clear();
subscribers.id.write().await.clear();
return Err(e);
}
(None, None) => {
debug!(
"Ignoring uninteresting message id {} (number: {})",
msg_id, msg_num
);
trace!("Contents: {:?}", response);
}
}
if remove_it_num {
subscribers.num.write().await.remove(&msg_num);
}
Ok(())
+17 -10
View File
@@ -2,7 +2,7 @@ use super::BcConnection;
use crate::bcmedia::codex::BcMediaCodex;
use crate::{bc::model::*, Error, Result};
use futures::stream::{IntoAsyncRead, Stream, StreamExt, TryStreamExt};
use std::io::Result as IoResult;
use std::io::{Error as IoError, ErrorKind, Result as IoResult};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::sync::mpsc::Receiver;
@@ -11,21 +11,21 @@ use tokio_util::codec::FramedRead;
use tokio_util::compat::{Compat, FuturesAsyncReadCompatExt};
pub struct BcSubscription<'a> {
rx: ReceiverStream<Bc>,
rx: ReceiverStream<Result<Bc>>,
msg_num: u32,
conn: &'a BcConnection,
}
pub struct BcStream<'a> {
rx: &'a mut ReceiverStream<Bc>,
rx: &'a mut ReceiverStream<Result<Bc>>,
}
impl<'a> Unpin for BcStream<'a> {}
impl<'a> Stream for BcStream<'a> {
type Item = Bc;
type Item = Result<Bc>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Bc>> {
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Result<Bc>>> {
let mut this = self.as_mut();
match Pin::new(&mut this.rx).poll_next(cx) {
Poll::Ready(Some(bc)) => Poll::Ready(Some(bc)),
@@ -36,7 +36,7 @@ impl<'a> Stream for BcStream<'a> {
}
pub struct BcPayloadStream<'a> {
rx: &'a mut ReceiverStream<Bc>,
rx: &'a mut ReceiverStream<Result<Bc>>,
}
impl<'a> Unpin for BcPayloadStream<'a> {}
@@ -49,7 +49,7 @@ impl<'a> Stream for BcPayloadStream<'a> {
cx: &mut Context<'_>,
) -> Poll<Option<IoResult<Vec<u8>>>> {
match Pin::new(&mut self.rx).poll_next(cx) {
Poll::Ready(Some(bc)) => {
Poll::Ready(Some(Ok(bc))) => {
match bc {
Bc {
body:
@@ -65,6 +65,7 @@ impl<'a> Stream for BcPayloadStream<'a> {
}
}
}
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(IoError::new(ErrorKind::Other, e)))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
@@ -74,7 +75,11 @@ impl<'a> Stream for BcPayloadStream<'a> {
pub type BcMediaStream<'b> = FramedRead<Compat<IntoAsyncRead<BcPayloadStream<'b>>>, BcMediaCodex>;
impl<'a> BcSubscription<'a> {
pub fn new(rx: Receiver<Bc>, msg_num: u32, conn: &'a BcConnection) -> BcSubscription<'a> {
pub fn new(
rx: Receiver<Result<Bc>>,
msg_num: u32,
conn: &'a BcConnection,
) -> BcSubscription<'a> {
BcSubscription {
rx: ReceiverStream::new(rx),
msg_num,
@@ -90,8 +95,10 @@ impl<'a> BcSubscription<'a> {
pub async fn recv(&mut self) -> Result<Bc> {
let bc = self.rx.next().await.ok_or(Error::DroppedSubscriber)?;
assert!(bc.meta.msg_num as u32 == self.msg_num);
Ok(bc)
if let Ok(bc) = &bc {
assert!(bc.meta.msg_num as u32 == self.msg_num);
}
bc
}
#[allow(unused)]