1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) The Diem Core Contributors
// SPDX-License-Identifier: Apache-2.0

use std::time::Duration;

cfg_async_or_blocking! {
    use crate::Result;
}

#[derive(Clone, Debug)]
pub struct Retry {
    max_retries: u32,
    delay: Duration,
}

impl Default for Retry {
    fn default() -> Self {
        Self::new(20, Duration::from_millis(500))
    }
}

impl Retry {
    pub fn new(max_retries: u32, delay: Duration) -> Self {
        Self { max_retries, delay }
    }

    pub fn none() -> Self {
        Self::new(0, Duration::ZERO)
    }

    pub fn max_retries(&self) -> u32 {
        self.max_retries
    }

    pub fn delay(&self) -> Duration {
        self.delay
    }

    cfg_async_or_blocking! {
        fn next_delay(&self, remaining_attempts: u32) -> Duration {
            self.delay * self.max_retries.saturating_sub(remaining_attempts)
        }
    }

    cfg_blocking! {
        pub(crate) fn retry<T, F>(&self, f: F) -> Result<T>
        where
            F: Fn() -> Result<T>,
        {
            let mut remaining_attempts = self.max_retries();
            loop {
                match f() {
                    Ok(r) => return Ok(r),
                    Err(error) if error.is_retriable() && remaining_attempts > 0 => {
                        remaining_attempts = remaining_attempts.saturating_sub(1);
                        std::thread::sleep(self.next_delay(remaining_attempts));
                    }
                    Err(error) => return Err(error),
                }
            }
        }
    }

    cfg_async! {
        pub(crate) async fn retry_async<T, F, O>(&self, f: F) -> Result<T>
        where
            F: Fn() -> O,
            O: std::future::Future<Output = Result<T>>,
        {
            let mut remaining_attempts = self.max_retries();
            loop {
                match f().await {
                    Ok(r) => return Ok(r),
                    Err(error) if error.is_retriable() && remaining_attempts > 0 => {
                        remaining_attempts = remaining_attempts.saturating_sub(1);
                        tokio::time::sleep(self.next_delay(remaining_attempts)).await;
                    }
                    Err(error) => return Err(error),
                }
            }
        }
    }
}