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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::io::Error;
use std::fmt;
use std::time::Instant;

use futures::{Async, Future, Poll};
use futures_timer::{Delay, TimerHandle};

use super::strategy::{Strategy, StrategyIter};
use super::action::Action;
use super::condition::Condition;

enum RetryState<A> where A: Action {
    Running(A::Future),
    Sleeping(Delay)
}

impl<A: Action> RetryState<A> {
    fn poll(&mut self) -> RetryFuturePoll<A> {
        match *self {
            RetryState::Running(ref mut future) =>
                RetryFuturePoll::Running(future.poll()),
            RetryState::Sleeping(ref mut future) =>
                RetryFuturePoll::Sleeping(future.poll())
        }
    }
}

enum RetryFuturePoll<A> where A: Action {
    Running(Poll<A::Item, A::Error>),
    Sleeping(Poll<(), Error>)
}

/// Future that drives multiple attempts at an action via a retry strategy.
pub struct Retry<A> where A: Action {
    retry_if: RetryIf<A, fn(&A::Error) -> bool>
}

impl<A: Action> Retry<A> {
    /// Creates a new retry future.
    pub fn new(strategy: &Strategy, action: A) -> Retry<A> {
        Retry::new_with_handle(TimerHandle::default(), strategy, action)
    }

    /// Creates a new retry future, using the provided `handle` to schedule timeouts.
    pub fn new_with_handle(handle: TimerHandle, strategy: &Strategy, action: A) -> Retry<A> {
        Retry {
            retry_if: RetryIf::new_with_handle(handle, strategy, action, (|_| true) as fn(&A::Error) -> bool)
        }
    }
}

impl<A: Action> fmt::Debug for Retry<A> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("Retry").finish()
    }
}

impl<A: Action> Future for Retry<A> {
    type Item = A::Item;
    type Error = A::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        self.retry_if.poll()
    }
}

/// Future that drives multiple attempts at an action via a retry strategy. Retries are only attempted if
/// the `Error` returned by the future satisfies a given condition.
pub struct RetryIf<A, C>
    where A: Action,
          C: Condition<A::Error>
{
    strategy_iter: StrategyIter,
    state: RetryState<A>,
    action: A,
    handle: TimerHandle,
    condition: C
}

impl<A, C> RetryIf<A, C>
    where A: Action,
          C: Condition<A::Error>
{
    /// Creates a new retry future.
    pub fn new(
        strategy: &Strategy,
        action: A,
        condition: C
    ) -> RetryIf<A, C> {
        RetryIf::new_with_handle(TimerHandle::default(), strategy, action, condition)
    }

    /// Creates a new retry future, using the provided `handle` to schedule timeouts.
    pub fn new_with_handle(
        handle: TimerHandle,
        strategy: &Strategy,
        mut action: A,
        condition: C
    ) -> RetryIf<A, C> {
        RetryIf {
            strategy_iter: strategy.iter(),
            state: RetryState::Running(action.run()),
            action: action,
            handle: handle,
            condition: condition,
        }
    }

    fn attempt(&mut self) -> Poll<A::Item, A::Error> {
        let future = self.action.run();
        self.state = RetryState::Running(future);
        self.poll()
    }

    fn retry(&mut self, err: A::Error) -> Poll<A::Item, A::Error> {
        match self.strategy_iter.next() {
            None => Err(err),
            Some(duration) => {
                let instant = Instant::now() + duration;
                let future = Delay::new_handle(instant, self.handle.clone());
                self.state = RetryState::Sleeping(future);
                self.poll()
            }
        }
    }
}

impl<A: Action, C: Condition<A::Error>> fmt::Debug for RetryIf<A, C> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("RetryIf").finish()
    }
}

impl<A, C> Future for RetryIf<A, C>
    where A: Action,
          C: Condition<A::Error>
{
    type Item = A::Item;
    type Error = A::Error;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        match self.state.poll() {
            RetryFuturePoll::Running(poll_result) => match poll_result {
                Ok(async) => Ok(async),
                Err(err) => {
                    if self.condition.should_retry(&err) {
                        self.retry(err)
                    } else {
                        Err(err)
                    }
                }
            },
            RetryFuturePoll::Sleeping(poll_result) => match poll_result.unwrap() {
                Async::NotReady => Ok(Async::NotReady),
                Async::Ready(_) => self.attempt()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;
    use futures::Future;
    use super::Strategy;

    #[test]
    fn attempts_just_once() {
        let s = Strategy::fixed(Duration::from_millis(100))
            .with_max_retries(0);
        let mut num_calls = 0;
        let res = {
            let fut = s.retry(|| {
                num_calls += 1;
                Err::<(), u64>(42)
            });
            fut.wait()
        };

        assert_eq!(res, Err(42));
        assert_eq!(num_calls, 1);
    }

    #[test]
    fn attempts_until_max_retries_exceeded() {
        let s = Strategy::fixed(Duration::from_millis(100))
            .with_max_retries(2);
        let mut num_calls = 0;
        let res = {
            let fut = s.retry(|| {
                num_calls += 1;
                Err::<(), u64>(42)
            });
            fut.wait()
        };

        assert_eq!(res, Err(42));
        assert_eq!(num_calls, 3);
    }

    #[test]
    fn attempts_until_success() {
        let s = Strategy::fixed(Duration::from_millis(100));
        let mut num_calls = 0;
        let res = {
            let fut = s.retry(|| {
                num_calls += 1;
                if num_calls < 4 {
                    Err::<(), u64>(42)
                } else {
                    Ok::<(), u64>(())
                }
            });
            fut.wait()
        };

        assert_eq!(res, Ok(()));
        assert_eq!(num_calls, 4);
    }

    #[test]
    fn attempts_retry_only_if_given_condition_is_true() {
        let s = Strategy::fixed(Duration::from_millis(100))
            .with_max_retries(5);
        let mut num_calls = 0;
        let res = {
            let action = || {
                num_calls += 1;
                Err::<(), u64>(num_calls)
            };
            let fut = s.retry_if(action, |e: &u64| *e < 3);
            fut.wait()
        };

        assert_eq!(res, Err(3));
        assert_eq!(num_calls, 3);
    }
}