Error feedback for the user upon communication errors

If communication with the other party fails the program should stop and the user should see the respective error.
Communication errors are handled in the event-loop. Upon a communication error the event loop is stopped.
Since the event loop is only stopped upon error the Result returned from the event loop is Infallible.

If one of the two futures, event loop and swap,  finishes (success/failure) the other future should be stopped as well.
We use tokio::selec! to stop either future if the other stops.
This commit is contained in:
Daniel Karzel 2021-02-26 16:11:14 +11:00
parent e66e84085b
commit bb1537d6f2
4 changed files with 65 additions and 32 deletions

View file

@ -7,10 +7,10 @@ use crate::{
bob::{Behaviour, OutEvent, QuoteRequest, State0, State2},
},
};
use anyhow::{anyhow, Result};
use anyhow::{anyhow, bail, Result};
use futures::FutureExt;
use libp2p::{core::Multiaddr, PeerId};
use std::sync::Arc;
use std::{convert::Infallible, sync::Arc};
use tokio::sync::mpsc::{Receiver, Sender};
use tracing::{debug, error, info};
@ -167,7 +167,7 @@ impl EventLoop {
Ok((event_loop, handle))
}
pub async fn run(mut self) {
pub async fn run(mut self) -> Result<Infallible> {
loop {
tokio::select! {
swarm_event = self.swarm.next().fuse() => {
@ -193,7 +193,7 @@ impl EventLoop {
}
OutEvent::ResponseSent => {}
OutEvent::CommunicationError(err) => {
error!("Communication error: {:#}", err)
bail!("Communication error: {:#}", err)
}
}
},