app_task/
backoff_strategy.rs1use std::marker::PhantomData;
2use std::time::Duration;
3
4pub mod constant_time;
5pub mod threshold_buckets;
6
7pub trait StrategyFactory: Send + Sync {
8 type Strategy: BackoffStrategy;
9
10 fn create_strategy(&self) -> Self::Strategy;
11}
12
13pub trait BackoffStrategy: Send + Sync + 'static {
14 fn add_failure(&mut self);
15
16 fn next_backoff(&self) -> Duration;
17}
18
19pub struct DefaultStrategyFactory<S> {
20 _strategy: PhantomData<S>,
21}
22
23impl<S> Default for DefaultStrategyFactory<S> {
24 fn default() -> Self {
25 Self {
26 _strategy: PhantomData,
27 }
28 }
29}
30
31impl<S> DefaultStrategyFactory<S> {
32 pub fn new() -> Self {
33 Self::default()
34 }
35}
36
37impl<S> StrategyFactory for DefaultStrategyFactory<S>
38where
39 S: Default + BackoffStrategy,
40{
41 type Strategy = S;
42
43 fn create_strategy(&self) -> Self::Strategy {
44 Self::Strategy::default()
45 }
46}