retry_async

Macro retry_async 

Source
macro_rules! retry_async {
    ($retry: expr, $code: tt) => { ... };
}
Expand description

Retry but for an async context

use xmtp_common::{retry_async, retry::{RetryableError, Retry}};
use thiserror::Error;
use tokio::sync::mpsc;

#[derive(Debug, Error)]
enum MyError {
    #[error("A retryable error")]
    Retryable,
    #[error("An error we don't want to retry")]
    NotRetryable
}

impl RetryableError for MyError {
    fn is_retryable(&self) -> bool {
        match self {
            Self::Retryable => true,
            _=> false,
        }
    }
}

async fn fallable_fn(rx: &mut mpsc::Receiver<usize>) -> Result<(), MyError> {
    if rx.recv().await.unwrap() == 2 {
        return Ok(());
    }
    Err(MyError::Retryable)
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), MyError> {

    let (tx, mut rx) = mpsc::channel(3);

    for i in 0..3 {
        tx.send(i).await.unwrap();
    }
    retry_async!(Retry::default(), (async {
        fallable_fn(&mut rx).await
    }))
}