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
use futures::future;
use tower::retry::Policy;

/// Re-export from [tower::retry::RetryLayer]
pub use tower::retry::RetryLayer;

use crate::{error::JobError, request::JobRequest, response::JobResult};

type Req<T> = JobRequest<T>;
type Res = JobResult;
type Err = JobError;

/// Retries a job instantly until max_attempts
#[derive(Clone, Debug)]
pub struct DefaultRetryPolicy;

impl<T> Policy<Req<T>, Res, Err> for DefaultRetryPolicy
where
    T: Clone,
{
    type Future = future::Ready<Self>;

    fn retry(&self, req: &Req<T>, result: Result<&Res, &Err>) -> Option<Self::Future> {
        match result {
            Ok(_) => {
                // Treat all `Response`s as success,
                // so don't retry...
                None
            }
            Err(_) if (req.max_attempts() - req.attempts() > 0) => {
                Some(future::ready(DefaultRetryPolicy))
            }
            Err(_) => None,
        }
    }

    fn clone_request(&self, req: &Req<T>) -> Option<Req<T>> {
        let mut req = req.clone();
        req.record_attempt();
        Some(req)
    }
}