Skip to content
Merged
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 7 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
[package]
name = "mulligan"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
description = "A flexible retry library for Rust async operations with configurable backoff strategies and jitter."
license = "MIT"
homepage = "https://github.com/theelderbeever/mulligan"
repository = "https://github.com/theelderbeever/mulligan"
documentation = "https://docs.rs/mulligan"
rust-version = "1.85"

[features]
default = ["tokio"] # Make tokio the default runtime
tokio = ["dep:tokio"] # Depend on tokio when this feature is enabled
async-std = ["dep:async-std"] # Depend on async-std when this feature is enabled
default = ["tokio"] # Make tokio the default runtime
tokio = ["dep:tokio"] # Depend on tokio when this feature is enabled
async-std = ["dep:async-std"] # Depend on async-std when this feature is enabled

[dependencies]
tokio = { version = "1", optional = true, features = ["time"] }
async-std = { version = "1", optional = true }
rand = { version = "0.8"}
rand = { version = "0.8" }

[[example]]
name = "hello_world"
Expand All @@ -26,4 +27,4 @@ required-features = ["tokio"]
tokio = { version = "1", features = ["full"] }

[dev-dependencies]
tokio = { version = "1", features = ["full"]}
tokio = { version = "1", features = ["full"] }
12 changes: 6 additions & 6 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ async fn main() {
.max_delay(Duration::from_secs(3))
.full_jitter()
.exponential(Duration::from_secs(1))
.execute(|| async { this_errors("hello").await })
.execute(async move || this_errors("hello").await)
.await
});
let world = tokio::spawn(async move {
mulligan::until(|r| r.is_ok())
.stop_after(10)
.jitter(mulligan::jitter::Full)
.fixed(Duration::from_secs(1))
.execute(|| async { this_errors("world").await })
.execute(async move || this_errors("world").await)
.await
});

Expand All @@ -30,8 +30,8 @@ async fn main() {
.stop_after(10)
.full_jitter()
.fixed(Duration::from_millis(200))
.after_attempt(|res, attempt| println!("Attempt = {}, result = {:?}", attempt, res))
.execute(|| async { this_errors("Oh uh!!!").await })
.after_attempt(|res, attempt| println!("Attempt = {attempt}, result = {res:?}"))
.execute(async move || this_errors("Oh uh!!!").await)
.await
});

Expand All @@ -44,8 +44,8 @@ async fn main() {
.stop_after(3)
.full_jitter()
.fixed(Duration::from_millis(200))
.after_attempt(|res, attempt| println!("Attempt = {}, result = {:?}", attempt, res))
.execute(|| async { this_errors("Uh oh!!!").await })
.after_attempt(|res, attempt| println!("Attempt = {attempt}, result = {res:?}"))
.execute(async move || this_errors("Uh oh!!!").await)
.await
})
.await;
Expand Down
15 changes: 7 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ compile_error!("At least on of 'tokio' or 'async-std' feature must be enabled");
pub mod backoff;
pub mod jitter;

use std::{future::Future, marker::PhantomData, time::Duration};
use std::{marker::PhantomData, time::Duration};

pub use backoff::{Backoff, Exponential, Fixed, Linear};
pub use jitter::{Decorrelated, Equal, Full, Jitter, NoJitter};
Expand Down Expand Up @@ -118,10 +118,9 @@ where
/// .await;
/// # }
/// ```
pub async fn execute<F, Fut>(mut self, f: F) -> Result<T, E>
pub async fn execute<F>(mut self, mut f: F) -> Result<T, E>
where
F: Fn() -> Fut + 'static,
Fut: Future<Output = Result<T, E>> + Send,
F: AsyncFnMut() -> Result<T, E> + 'static,
{
let mut attempt: u32 = 0;
loop {
Expand All @@ -131,7 +130,7 @@ where

let res = f().await;

if self.stop_after.map_or(false, |max| attempt >= max) | (self.until)(&res) {
if self.stop_after.is_some_and(|max| attempt >= max) | (self.until)(&res) {
return res;
}

Expand Down Expand Up @@ -166,9 +165,9 @@ where
/// .execute_sync(move || { this_errors("hello") });
/// # }
/// ```
pub fn execute_sync<F>(mut self, f: F) -> Result<T, E>
pub fn execute_sync<F>(mut self, mut f: F) -> Result<T, E>
where
F: Fn() -> Result<T, E>,
F: FnMut() -> Result<T, E>,
{
let mut attempt: u32 = 0;
loop {
Expand All @@ -178,7 +177,7 @@ where

let res = f();

if self.stop_after.map_or(false, |max| attempt >= max) | (self.until)(&res) {
if self.stop_after.is_some_and(|max| attempt >= max) | (self.until)(&res) {
return res;
}

Expand Down
Loading