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
use std::marker::PhantomData;
use std::time::Duration;

pub mod constant_time;
pub mod threshold_buckets;

pub trait StrategyFactory: Send + Sync {
    type Strategy: BackoffStrategy;

    fn create_strategy(&self) -> Self::Strategy;
}

pub trait BackoffStrategy: Send + Sync + 'static {
    fn add_failure(&mut self);

    fn next_backoff(&self) -> Duration;
}

pub struct DefaultStrategyFactory<S> {
    _strategy: PhantomData<S>,
}

impl<S> Default for DefaultStrategyFactory<S> {
    fn default() -> Self {
        Self {
            _strategy: PhantomData,
        }
    }
}

impl<S> DefaultStrategyFactory<S> {
    pub fn new() -> Self {
        Self::default()
    }
}

impl<S> StrategyFactory for DefaultStrategyFactory<S>
where
    S: Default + BackoffStrategy,
{
    type Strategy = S;

    fn create_strategy(&self) -> Self::Strategy {
        Self::Strategy::default()
    }
}