tokio_retry2/strategy/
max_interval.rs

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
use std::time::Instant;
use tokio::time::Duration;

/// Wraps a strategy, applying `max_interval``, after which strategy will
/// stop retrying.
pub trait MaxInterval: Iterator<Item = Duration> {
    /// Applies a `max_interval` for a strategy. Same as  `max_duration`, but using millis instead of `Duration`.
    fn max_interval(self, max_interval: u64) -> MaxIntervalIterator<Self>
    where
        Self: Sized,
    {
        MaxIntervalIterator {
            iter: self,
            start: Instant::now(),
            max_duration: Duration::from_millis(max_interval),
        }
    }

    /// Applies a `max_duration` for a strategy. In `max_duration` from now,
    /// the strategy will stop retrying. If `max_duration` is passed, the strategy
    /// will stop retrying after `max_duration` is reached.
    fn max_duration(self, max_duration: Duration) -> MaxIntervalIterator<Self>
    where
        Self: Sized,
    {
        MaxIntervalIterator {
            iter: self,
            start: Instant::now(),
            max_duration,
        }
    }
}

impl<I> MaxInterval for I where I: Iterator<Item = Duration> {}

/// A strategy wrapper with applied max_interval,
/// created by [`MaxInterval::max_interval`] function.
#[derive(Debug)]
pub struct MaxIntervalIterator<I> {
    iter: I,
    start: Instant,
    max_duration: Duration,
}

impl<I: Iterator<Item = Duration>> Iterator for MaxIntervalIterator<I> {
    type Item = Duration;

    fn next(&mut self) -> Option<Self::Item> {
        if self.start.elapsed() > self.max_duration {
            #[cfg(feature = "tracing")]
            tracing::warn!("`max_duration` reached, cancelling retry");

            None
        } else {
            self.iter.next()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::strategy::FixedInterval;

    #[tokio::test]
    async fn returns_none_after_max_interval_passes() {
        let mut s = FixedInterval::from_millis(10).max_interval(50);
        assert_eq!(s.next(), Some(Duration::from_millis(10)));
        tokio::time::sleep(Duration::from_millis(15)).await;
        assert_eq!(s.next(), Some(Duration::from_millis(10)));
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!(s.next(), None);
    }

    #[tokio::test]
    async fn returns_none_after_max_duration_passes() {
        let mut s = FixedInterval::from_millis(10).max_duration(Duration::from_millis(50));
        assert_eq!(s.next(), Some(Duration::from_millis(10)));
        tokio::time::sleep(Duration::from_millis(15)).await;
        assert_eq!(s.next(), Some(Duration::from_millis(10)));
        tokio::time::sleep(Duration::from_millis(100)).await;
        assert_eq!(s.next(), None);
    }
}