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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use std::time::Duration;
use tokio::time::timeout;

pub struct RetryResult<T, E> {
    pub success: Option<T>,
    pub errors: Vec<E>,
    pub timeout_count: u64,
}

pub async fn execute_retry<T, E, Fut>(
    max_try_count: u64,
    retry_duration: Duration,
    timeout_duration: Duration,
    inner: impl Fn(u64) -> Fut,
) -> RetryResult<T, E>
where
    Fut: std::future::Future<Output = Result<T, E>>,
{
    execute_retry_with_exponential_backoff(max_try_count, retry_duration, timeout_duration, inner, false).await
}

pub async fn execute_retry_with_exponential_backoff<T, E, Fut>(
    max_try_count: u64,
    retry_duration: Duration,
    timeout_duration: Duration,
    inner: impl Fn(u64) -> Fut,
    exponential_backoff: bool,
) -> RetryResult<T, E>
where
    Fut: std::future::Future<Output = Result<T, E>>,
{
    let mut try_count = 0;
    let mut timeout_count = 0;
    let mut errors = vec![];
    loop {
        try_count += 1;
        if timeout_duration.is_zero() {
            match inner(try_count).await {
                Ok(res) => {
                    return RetryResult {
                        success: Some(res),
                        errors,
                        timeout_count,
                    }
                }
                Err(err) => {
                    errors.push(err);
                    
                }
            }
        } else {
            match timeout(timeout_duration, inner(try_count)).await {
                Ok(res) => match res {
                    Ok(res) => {
                        return RetryResult {
                            success: Some(res),
                            errors,
                            timeout_count,
                        }
                    }
                    Err(err) => {
                        errors.push(err);
                    }
                },
                Err(_) => {
                    timeout_count += 1;
                }
            }
        }
        if try_count >= max_try_count {
            return RetryResult {
                success: None,
                errors,
                timeout_count,
            };
        }
        if !retry_duration.is_zero() {
            let duration = if exponential_backoff {
                retry_duration.mul_f64(2_i32.pow(try_count as u32) as f64)
            } else {
                retry_duration
            };
            tokio::time::sleep(duration).await;
        }
    }
}

#[cfg(test)]
mod tests {
    use std::vec;

    use tokio::time::sleep;

    use super::*;
    // REALM_CODE=test cargo test test_retry -- --nocapture --test-threads=1

    async fn inner_success() -> Result<usize, String> {
        Ok(1)
    }

    async fn inner_fail(n: u64) -> Result<usize, String> {
        println!("inner_fail {}", n);
        Err("error".to_string())
    }

    async fn inner_later() -> Result<usize, String> {
        sleep(Duration::from_millis(100)).await;
        Ok(1)
    }

    async fn inner_complex(n: u64) -> Result<usize, String> {
        if n == 3 {
            Ok(1)
        } else {
            Err("error".to_string())
        }
    }

    #[tokio::test]
    async fn test_retry() -> anyhow::Result<()> {
        // Success
        let res = execute_retry(
            3,
            Duration::from_secs(0),
            Duration::from_secs(0),
            |_n| async { inner_success().await },
        )
        .await;
        assert_eq!(res.success, Some(1));
        assert_eq!(res.errors.len(), 0);
        assert_eq!(res.timeout_count, 0);

        // Failure
        let res = execute_retry_with_exponential_backoff(
            3,
            Duration::from_secs(1),
            Duration::from_secs(0),
            |n| async move { inner_fail(n).await },
            true,
        )
        .await;
        assert_eq!(res.success, None);
        assert_eq!(
            res.errors,
            vec!["error".to_owned(), "error".to_owned(), "error".to_owned(),]
        );
        assert_eq!(res.timeout_count, 0);

        // Timeout
        let res = execute_retry(
            3,
            Duration::from_secs(0),
            Duration::from_millis(10),
            |_n| async { inner_later().await },
        )
        .await;
        assert_eq!(res.success, None);
        assert_eq!(res.errors.len(), 0);
        assert_eq!(res.timeout_count, 3);

        // Complex
        let res = execute_retry(
            3,
            Duration::from_secs(0),
            Duration::from_secs(0),
            |n| async move { inner_complex(n).await },
        )
        .await;
        assert_eq!(res.success, Some(1));
        assert_eq!(res.errors, vec!["error".to_owned(), "error".to_owned()]);
        assert_eq!(res.timeout_count, 0);

        Ok(())
    }
}