Skip to content

Commit

Permalink
Rename fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonqn committed Sep 26, 2021
1 parent 953d267 commit 81d8eb6
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 54 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.2.0"
version = "0.3.0"
edition = "2018"
authors = ["Ilya Titkov <[email protected]>"]
keywords = ["futures-retry", "retry", "futures"]
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ At most 4 requests will be fired.

When one of runninng requests completes with an [`Ok`] result it will be returned.
```rust
use fure::policies::{interval, retry_failed};
use fure::policies::{interval, failed};
use std::time::Duration;

let get_body = || async {
Expand All @@ -26,14 +26,14 @@ let get_body = || async {
.text()
.await
};
let policy = retry_failed(interval(Duration::from_secs(1)), 3);
let policy = failed(interval(Duration::from_secs(1)), 3);
let body = fure::retry(get_body, policy).await?;
println!("body = {}", body);
```
### Sequential retry with backoff.
Retries failed requests with an exponential backoff and a jitter.
```rust
use fure::{policies::{backoff, retry_if}, backoff::exponential};
use fure::{policies::{backoff, cond}, backoff::exponential};
use std::time::Duration;

let get_body = || async {
Expand All @@ -44,7 +44,7 @@ let get_body = || async {
};
let exp_backoff = exponential(Duration::from_secs(1), 2, Some(Duration::from_secs(10)))
.map(fure::backoff::jitter);
let policy = retry_if(backoff(exp_backoff), |result| !matches!(result, Some(Ok(_))));
let policy = cond(backoff(exp_backoff), |result| !matches!(result, Some(Ok(_))));
let body = fure::retry(get_body, policy).await?;
println!("body = {}", body);
```
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! When one of runninng requests completes with an [`Ok`] result it will be returned.
//! ```
//! # async fn run() -> Result<(), reqwest::Error> {
//! use fure::policies::{interval, retry_failed};
//! use fure::policies::{interval, failed};
//! use std::time::Duration;
//!
//! let get_body = || async {
Expand All @@ -25,7 +25,7 @@
//! .text()
//! .await
//! };
//! let policy = retry_failed(interval(Duration::from_secs(1)), 3);
//! let policy = failed(interval(Duration::from_secs(1)), 3);
//! let body = fure::retry(get_body, policy).await?;
//! println!("body = {}", body);
//! # Ok(())
Expand All @@ -35,7 +35,7 @@
//! Retries failed requests with an exponential backoff.
//! ```
//! # async fn run() -> Result<(), reqwest::Error> {
//! use fure::{policies::{backoff, retry_if}, backoff::exponential};
//! use fure::{policies::{backoff, cond}, backoff::exponential};
//! use std::time::Duration;
//!
//! let get_body = || async {
Expand All @@ -45,7 +45,7 @@
//! .await
//! };
//! let exp_backoff = exponential(Duration::from_secs(1), 2, Some(Duration::from_secs(10)));
//! let policy = retry_if(backoff(exp_backoff), |result| !matches!(result, Some(Ok(_))));
//! let policy = cond(backoff(exp_backoff), |result| !matches!(result, Some(Ok(_))));
//! let body = fure::retry(get_body, policy).await?;
//! println!("body = {}", body);
//! # Ok(())
Expand Down Expand Up @@ -76,15 +76,15 @@ mod future;
/// Runs at most 4 concurrent futures and waits a successful one.
/// ```
/// # async fn run() -> Result<(), reqwest::Error> {
/// use fure::policies::{parallel, retry_failed};
/// use fure::policies::{parallel, failed};
///
/// let get_body = || async {
/// reqwest::get("https://www.rust-lang.org")
/// .await?
/// .text()
/// .await
/// };
/// let body = fure::retry(get_body, retry_failed(parallel(), 3)).await?;
/// let body = fure::retry(get_body, failed(parallel(), 3)).await?;
/// println!("body = {}", body);
/// # Ok(())
/// # }
Expand Down
28 changes: 11 additions & 17 deletions src/policies/concurrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ use crate::Policy;
///
/// ```
/// # async fn run() -> Result<(), reqwest::Error> {
/// use fure::policies::{parallel, retry_failed};
/// use fure::policies::{parallel, failed};
///
/// let get_body = || async {
/// reqwest::get("https://www.rust-lang.org")
/// .await?
/// .text()
/// .await
/// };
/// let body = fure::retry(get_body, retry_failed(parallel(), 3)).await?;
/// let body = fure::retry(get_body, failed(parallel(), 3)).await?;
/// println!("body = {}", body);
/// # Ok(())
/// # }
Expand Down Expand Up @@ -59,7 +59,7 @@ mod delayed {
/// If request takes less than 1 second no next futures will be run.
/// ```
/// # async fn run() -> Result<(), reqwest::Error> {
/// use fure::policies::{interval, retry_failed};
/// use fure::policies::{interval, failed};
/// use std::time::Duration;
///
/// let get_body = || async {
Expand All @@ -68,7 +68,7 @@ mod delayed {
/// .text()
/// .await
/// };
/// let body = fure::retry(get_body, retry_failed(interval(Duration::from_secs(1)), 4)).await?;
/// let body = fure::retry(get_body, failed(interval(Duration::from_secs(1)), 4)).await?;
/// println!("body = {}", body);
/// # Ok(())
/// # }
Expand Down Expand Up @@ -102,7 +102,7 @@ pub use delayed::*;
mod test {
use std::sync::{Arc, Mutex};

use crate::policies::retry_failed;
use crate::policies::failed;
use crate::retry;
use crate::tests::run_test;
use std::future::pending;
Expand Down Expand Up @@ -136,7 +136,7 @@ mod test {
}
};

let result = retry(create_fut, retry_failed(parallel(), 4)).await;
let result = retry(create_fut, failed(parallel(), 4)).await;

let guard = call_count.lock().unwrap();
assert_eq!(*guard, 4);
Expand All @@ -161,7 +161,7 @@ mod test {
}
};

let result = retry(create_fut, retry_failed(parallel(), 5)).await;
let result = retry(create_fut, failed(parallel(), 5)).await;

let guard = call_count.lock().unwrap();
assert_eq!(*guard, 2);
Expand Down Expand Up @@ -194,11 +194,8 @@ mod test {
}
};

let result = retry(
create_fut,
retry_failed(interval(Duration::from_secs(10000)), 2),
)
.await;
let result =
retry(create_fut, failed(interval(Duration::from_secs(10000)), 2)).await;

let guard = call_count.lock().unwrap();
assert_eq!(*guard, 3);
Expand All @@ -224,11 +221,8 @@ mod test {
}
};
let now = Instant::now();
let result = retry(
create_fut,
retry_failed(interval(Duration::from_millis(50)), 2),
)
.await;
let result =
retry(create_fut, failed(interval(Duration::from_millis(50)), 2)).await;

let guard = call_count.lock().unwrap();
assert_eq!(*guard, 2);
Expand Down
32 changes: 16 additions & 16 deletions src/policies/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ pub use concurrent::*;
pub use sequential::*;

/// Creates a policy to retry failed futures specified number of times
pub fn retry_failed<P>(policy: P, max_retries: usize) -> RetryAttempts<P, usize> {
pub fn failed<P>(policy: P, max_retries: usize) -> RetryAttempts<P, usize> {
RetryAttempts {
policy,
condition: max_retries,
}
}

/// Returns a policy to retry futures while `retry_if` returns `true`
pub fn retry_if<P, T, E, FN>(policy: P, retry_if: FN) -> RetryAttempts<P, FN>
/// Returns a policy to retry futures while `cond` returns `true`
pub fn cond<P, T, E, FN>(policy: P, cond: FN) -> RetryAttempts<P, FN>
where
FN: FnMut(Option<Result<&T, &E>>) -> bool,
{
RetryAttempts {
policy,
condition: retry_if,
condition: cond,
}
}

/// An policy created by [`retry_failed`] and [`retry_if`].
/// An policy created by [`failed`] and [`cond`].
pub struct RetryAttempts<P, C> {
policy: P,
condition: C,
Expand Down Expand Up @@ -120,11 +120,11 @@ where
#[cfg(test)]
mod tests {

mod retry_failed {
mod failed {
use std::sync::{Arc, Mutex};

use crate::{
policies::{retry_failed, sequential},
policies::{failed, sequential},
retry,
tests::run_test,
};
Expand All @@ -140,9 +140,9 @@ mod tests {
Err::<(), ()>(())
};
let policy = sequential();
let retry_if = retry_failed(policy, 2);
let cond = failed(policy, 2);

let result = retry(create_fut, retry_if).await;
let result = retry(create_fut, cond).await;

let guard = call_count.lock().unwrap();
assert_eq!(*guard, 3);
Expand All @@ -161,9 +161,9 @@ mod tests {
Ok::<(), ()>(())
};
let policy = sequential();
let retry_if = retry_failed(policy, 2);
let cond = failed(policy, 2);

let result = retry(create_fut, retry_if).await;
let result = retry(create_fut, cond).await;

let guard = call_count.lock().unwrap();
assert_eq!(*guard, 1);
Expand All @@ -172,17 +172,17 @@ mod tests {
}
}

mod retry_if {
mod cond {
use std::sync::{Arc, Mutex};

use crate::{
policies::{retry_if, sequential},
policies::{cond, sequential},
retry,
tests::run_test,
};

#[test]
fn should_retry_if_returns_true() {
fn should_cond_returns_true() {
run_test(async move {
let call_count = Arc::new(Mutex::new(0));
let create_fut = || async {
Expand All @@ -193,12 +193,12 @@ mod tests {
};
let policy = sequential();
let mut tries_left = 3;
let retry_if = retry_if(policy, |result| {
let cond = cond(policy, |result| {
tries_left -= 1;
tries_left != 0 && !matches!(result, Some(Ok(_)))
});

let result = retry(create_fut, retry_if).await;
let result = retry(create_fut, cond).await;

let guard = call_count.lock().unwrap();
assert_eq!(*guard, 3);
Expand Down
20 changes: 10 additions & 10 deletions src/policies/sequential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ use crate::Policy;
/// Sends at most 4 requests and returns the first [`Ok`] result.
/// ```
/// # async fn run() -> Result<(), reqwest::Error> {
/// use fure::policies::{sequential, retry_failed};
/// use fure::policies::{sequential, failed};
///
/// let get_body = || async {
/// reqwest::get("https://www.rust-lang.org")
/// .await?
/// .text()
/// .await
/// };
/// let body = fure::retry(get_body, retry_failed(sequential(), 3)).await?;
/// let body = fure::retry(get_body, failed(sequential(), 3)).await?;
/// println!("body = {}", body);
/// # Ok(())
/// # }
Expand Down Expand Up @@ -59,7 +59,7 @@ mod retry_backoff {
/// Each next request will be sent only after specified backoff.
/// ```
/// # async fn run() -> Result<(), reqwest::Error> {
/// use fure::{backoff::fixed, policies::{backoff, retry_failed}};
/// use fure::{backoff::fixed, policies::{backoff, failed}};
/// use std::time::Duration;
///
/// let get_body = || async {
Expand All @@ -69,7 +69,7 @@ mod retry_backoff {
/// .await
/// };
/// let fixed = fixed(Duration::from_secs(3));
/// let body = fure::retry(get_body, retry_failed(backoff(fixed), 3)).await?;
/// let body = fure::retry(get_body, failed(backoff(fixed), 3)).await?;
/// println!("body = {}", body);
/// # Ok(())
/// # }
Expand Down Expand Up @@ -137,7 +137,7 @@ pub use retry_backoff::*;
mod tests {
use std::sync::{Arc, Mutex};

use crate::policies::retry_failed;
use crate::policies::failed;
use crate::retry;
use crate::tests::run_test;
mod retry_backoff {
Expand All @@ -161,15 +161,15 @@ mod tests {
2,
Some(Duration::from_secs(1)),
));
let result = retry(create_fut, retry_failed(policy, 2)).await;
let result = retry(create_fut, failed(policy, 2)).await;

assert!(now.elapsed() > Duration::from_millis(150));
assert!(result.is_err());
})
}
}

mod retry_failed {
mod failed {
use std::time::Duration;

use crate::policies::sequential::sequential;
Expand Down Expand Up @@ -206,9 +206,9 @@ mod tests {
}
};

crate::tests::spawn(async move {
retry(create_fut, retry_failed(sequential(), 2)).await
});
crate::tests::spawn(
async move { retry(create_fut, failed(sequential(), 2)).await },
);
crate::sleep::sleep(Duration::from_millis(10)).await;

{
Expand Down

0 comments on commit 81d8eb6

Please sign in to comment.