Skip to content

Commit

Permalink
Backoff policy stops if backoff iterator ends
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonqn committed Dec 2, 2021
1 parent 1f2fdd6 commit 8945c35
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fure"
version = "0.5.0"
version = "0.6.0"
edition = "2021"
authors = ["Ilya Titkov <[email protected]>"]
keywords = ["futures-retry", "retry", "futures"]
Expand Down
27 changes: 21 additions & 6 deletions src/policies/sequential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ mod retry_backoff {

/// Creates a policy to run futures sequentially with specified backoff.
///
/// Note: this policy has no stop condition, so for getting a result you should wrap it with [attempts](`super::super::attempts`), [cond](`super::super::cond`) or your own wrapper.
/// Note: this policy has no stop condition based on result, it retries while backoff iterator returns elements, so for getting the desired result you should wrap it with [attempts](`super::super::attempts`), [cond](`super::super::cond`) or your own wrapper.
/// ## Example
/// Sends at most 4 requests and returns the first [`Ok`] result.
///
Expand Down Expand Up @@ -100,7 +100,7 @@ mod retry_backoff {
}

fn retry(mut self, _result: Option<Result<&T, &E>>) -> Option<Self::RetryFuture> {
let delay = self.backoff.next().map(crate::sleep::sleep);
let delay = self.backoff.next().map(crate::sleep::sleep)?;
Some(SeqDelay {
backoff: Some(self.backoff),
delay,
Expand All @@ -114,7 +114,7 @@ mod retry_backoff {
{
backoff: Option<I>,
#[pin]
delay: Option<crate::sleep::Sleep>,
delay: crate::sleep::Sleep,
}
}

Expand All @@ -126,9 +126,9 @@ mod retry_backoff {

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
match this.delay.as_pin_mut().map(|x| x.poll(cx)) {
Some(Poll::Pending) => Poll::Pending,
_ => Poll::Ready(backoff(
match this.delay.poll(cx) {
Poll::Pending => Poll::Pending,
Poll::Ready(_) => Poll::Ready(backoff(
this.backoff.take().expect("SeqDelay Backoff must be some"),
)),
}
Expand Down Expand Up @@ -172,6 +172,21 @@ mod tests {
assert!(result.is_err());
})
}

#[test]
fn should_stop_retrying_when_backoff_iter_exhausted() {
run_test(async {
let create_fut = || async {
crate::tests::yield_now().await;
Err::<(), ()>(())
};

let policy = backoff(std::iter::empty());
let result = retry(create_fut, policy).await;

assert!(result.is_err())
})
}
}

mod attempts {
Expand Down

0 comments on commit 8945c35

Please sign in to comment.