Skip to main content

code0_flow/flow_service/
retry.rs

1use tokio::time::{Duration, sleep};
2use tonic::transport::{Channel, Endpoint};
3
4const MAX_BACKOFF: u64 = 2000 * 60;
5const MAX_RETRIES: i8 = 10;
6
7// Will create a channel and retry if its not possible
8pub async fn create_channel_with_retry(channel_name: &str, url: String) -> Channel {
9    let mut backoff = 100;
10    let mut retries = 0;
11
12    loop {
13        let channel = match Endpoint::from_shared(url.clone()) {
14            Ok(c) => {
15                log::debug!("Creating a new endpoint for the: {} Service", channel_name);
16                c.connect_timeout(Duration::from_secs(2))
17                    .timeout(Duration::from_secs(10))
18            }
19            Err(err) => {
20                panic!(
21                    "Cannot create Endpoint for Service: `{}`. Reason: {:?}",
22                    channel_name, err
23                );
24            }
25        };
26
27        match channel.connect().await {
28            Ok(ch) => {
29                return ch;
30            }
31            Err(err) => {
32                log::warn!(
33                    "Retry connect to `{}` using url: `{}` failed: {:?}, retrying in {}ms",
34                    channel_name,
35                    url,
36                    err,
37                    backoff
38                );
39                sleep(Duration::from_millis(backoff)).await;
40
41                backoff = (backoff * 2).min(MAX_BACKOFF);
42                retries += 1;
43
44                if retries >= MAX_RETRIES {
45                    panic!("Reached max retries to url {}", url)
46                }
47            }
48        }
49    }
50}