trait_net/retry/
policy.rs

1use super::{Decision, Policy};
2use std::time::Duration;
3
4#[derive(Clone, Copy, Debug, Default)]
5pub struct Once;
6
7impl Once {
8    pub fn new() -> Self {
9        Self
10    }
11}
12
13#[allow(unused_variables)]
14impl<Response> Policy<Response> for Once {
15    fn decide(&mut self, output: &Response) -> Decision {
16        Decision::Break
17    }
18}
19
20#[derive(Clone, Copy, Debug)]
21pub struct RetryOnError {
22    retry_attempts: usize,
23    delay: Duration,
24}
25
26impl RetryOnError {
27    pub fn new(retry_attempts: usize, delay: Duration) -> Self {
28        Self {
29            retry_attempts,
30            delay,
31        }
32    }
33}
34
35impl<Response, Error> Policy<Result<Response, Error>> for RetryOnError {
36    fn decide(&mut self, response: &Result<Response, Error>) -> Decision {
37        if response.is_ok() {
38            Decision::Break
39        } else if self.retry_attempts > 0 {
40            self.retry_attempts -= 1;
41            Decision::Retry(self.delay)
42        } else {
43            Decision::Break
44        }
45    }
46}