Skip to content

PR for Asyn support #41 #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
doc
target
.idea/
Cargo.lock
7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ license = "MIT"
name = "retry"
readme = "README.md"
repository = "https://github.com/jimmycuadra/retry"
version = "2.0.0"
version = "2.0.1"

[dependencies]
rand = { version = "^0.8", optional = true }
futures-timer = { version = "3", optional = true }

[dev-dependencies]
futures = "0.3"

[features]
default = ["random"]
random = ["rand"]
async = ["futures-timer"]
130 changes: 109 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ assert!(result.is_ok());
//! # Features
//!
//! - `random`: offer some random delay utilities (on by default)
//! - `async`: provide async version of retry

#![deny(missing_debug_implementations, missing_docs, warnings)]

Expand All @@ -132,6 +133,12 @@ use std::{
time::Duration,
};

#[cfg(feature = "async")]
use std::future::Future;

#[cfg(feature = "async")]
use futures_timer::Delay;

pub mod delay;
mod opresult;

Expand All @@ -158,33 +165,114 @@ where
O: FnMut(u64) -> OR,
OR: Into<OperationResult<R, E>>,
{
let mut iterator = iterable.into_iter();
let mut current_try = 1;
let mut total_delay = Duration::default();
let mut ctx = RetryContext::new(iterable.into_iter());

loop {
let result = ctx.check(operation(ctx.current_try).into());
match result {
Ok(v) => return ctx.make_result(v),
Err(delay) => sleep(delay),
}
}
}

/// Asynchronously retry the given operation until it succeeds, or until the given [`Duration`]
/// iterator ends.
///
/// # Usage
///
/// `retry_async` has exact parameters like [`retry`], but returns an future object instead.
///
/// ```rust
/// # use retry::{retry_async, delay::Fixed};
/// let mut collection = vec![1, 2, 3].into_iter();
///
/// let future = retry_async(Fixed::from_millis(100), || {
/// let result = match collection.next() {
/// Some(n) if n == 3 => Ok("n is 3!"),
/// Some(_) => Err("n must be 3!"),
/// None => Err("n was never 3!"),
/// };
/// async move { result }
/// });
/// let result = futures::executor::block_on(future);
///
/// assert!(result.is_ok());
/// ```
///
#[cfg(feature = "async")]
pub async fn retry_async<I, O, R, E, FOR, OR>(iterable: I, mut operation: O) -> Result<R, Error<E>>
where
I: IntoIterator<Item = Duration>,
O: FnMut() -> FOR,
FOR: Future<Output=OR>,
OR: Into<OperationResult<R, E>>,
{
retry_with_index_async(iterable, |_| operation()).await
}

/// Asynchronously retry the given operation until it succeeds, or until the given [`Duration`]
/// iterator ends, with each iteration of the operation receiving the number of the attempt as an
/// argument.
#[cfg(feature = "async")]
pub async fn retry_with_index_async<I, O, R, E, FOR, OR>(iterable: I, mut operation: O) -> Result<R, Error<E>>
where
I: IntoIterator<Item = Duration>,
O: FnMut(u64) -> FOR,
FOR: Future<Output=OR>,
OR: Into<OperationResult<R, E>>,
{
let mut ctx = RetryContext::new(iterable.into_iter());

loop {
match operation(current_try).into() {
OperationResult::Ok(value) => return Ok(value),
let result = ctx.check(operation(ctx.current_try).await.into());
match result {
Ok(v) => return ctx.make_result(v),
Err(delay) => Delay::new(delay).await,
}
}
}

/// Internal retry counting state
struct RetryContext<I: Iterator<Item=Duration>> {
iterator: I,
current_try: u64,
total_delay: Duration
}

impl<I> RetryContext<I> where I: Iterator<Item=Duration> {
fn new(iterator: I) -> RetryContext<I> {
Self { iterator, current_try: 1, total_delay: Duration::default() }
}

fn check<R,E>(&mut self, result: OperationResult<R,E>) -> Result<OperationResult<R, E>, Duration> {
match result {
OperationResult::Retry(error) => {
if let Some(delay) = iterator.next() {
sleep(delay);
current_try += 1;
total_delay += delay;
if let Some(delay) = self.iterator.next() {
self.current_try += 1;
self.total_delay += delay;
Err(delay)
} else {
return Err(Error {
error,
total_delay,
tries: current_try,
});
Ok(OperationResult::Err(error))
}
}
OperationResult::Err(error) => {
return Err(Error {
error,
total_delay,
tries: current_try,
});
}
v => Ok(v)
}
}

fn make_result<R,E>(self, result: OperationResult<R,E>) -> Result<R, Error<E>> {
match result {
OperationResult::Ok(v) => Ok(v),
OperationResult::Retry(error) => Err(self.make_error(error)),
OperationResult::Err(error) => Err(self.make_error(error)),
}
}

fn make_error<E>(&self, error: E) -> Error<E> {
Error {
error,
total_delay: self.total_delay,
tries: self.current_try,
}
}
}
Expand Down