Skip to content

Commit

Permalink
docs: document custom Sleeper impl (#147)
Browse files Browse the repository at this point in the history
### What does this PR do

Adds an example for user-provided sleeper implementation.

Closes #129
  • Loading branch information
SteveLauC authored Sep 5, 2024
1 parent 34c8e56 commit b3ca11e
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 6 deletions.
43 changes: 43 additions & 0 deletions backon/src/docs/examples/custom_sleeper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Let's implement a custom async Sleeper, say you are using Monoio as your async
runtime, you may want to implement it with `monoio::time::sleep()`. If you want
to implement a custom blocking Sleeper, you will find it pretty similar.

```rust
use std::time::Duration;
use backon::Sleeper;

/// Sleeper implemented using `monoio::time::sleep()`.
struct MonoioSleeper;

impl Sleeper for MonoioSleeper {
type Sleep = monoio::time::Sleep;

fn sleep(&self, dur: Duration) -> Self::Sleep {
monoio::time::sleep(dur)
}
}
```

Then you can use it like:

```rust
use backon::ExponentialBuilder;
use backon::Retryable;
use anyhow::Result;

async fn fetch() -> Result<String> {
Ok("Hello, World!".to_string())
}

#[monoio::main(timer_enabled = true)]
async fn main() -> Result<()> {
let content = fetch
.retry(ExponentialBuilder::default())
.sleep(MonoioSleeper)
.await?;

println!("fetch succeeded: {}", content);
Ok(())
}

```
49 changes: 43 additions & 6 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,53 @@
//!
//! # Sleep
//!
//! Retry in BackON requires an implementation for sleeping. BackON will accept a [`Sleeper`] to pause for a specified duration.
//! Retry in BackON requires an implementation for sleeping, such an implementation
//! is called a Sleeper, it will implement [`Sleeper`] or [`BlockingSleeper`] depending
//! on if it is going to be used in an asynchronous context.
//!
//! BackON employs the following default sleep implementations:
//! ## Default Sleeper
//!
//! - `tokio-sleep`: Utilizes [`TokioSleeper`] within a Tokio context in non-wasm32 environments.
//! - `gloo-timers-sleep`: Utilizes [`GlooTimersSleep`] to pause in wasm32 environments.
//! Currently, BackON has 3 built-in Sleeper implementations for different
//! environments, they are gated under their own features, which are enabled
//! by default:
//!
//! Users CAN provide a custom implementation if they prefer not to use the default options.
//! | `Sleeper` | feature | Environment | Asynchronous |
//! |---------------------|--------------------|-------------|---------------|
//! | [`TokioSleeper`] | tokio-sleep | non-wasm32 | Yes |
//! | [`GlooTimersSleep`] | gloo-timers-sleep | wasm32 | Yes |
//! | [`StdSleeper`] | std-blocking-sleep | all | No |
//!
//! If neither feature is enabled nor a custom implementation is provided, BackON will fallback to an empty sleeper. This will cause a panic in the `debug` profile and do nothing in the `release` profile.
//! ## Custom Sleeper
//!
//! If you do not want to use the built-in Sleeper, you CAN provide a custom
//! implementation, let's implement an asynchronous dummy Sleeper that does
//! not sleep at all. You will find it pretty similar when you implement a
//! blocking one.
//!
//! ```
//! use std::time::Duration;
//! use backon::Sleeper;
//!
//! /// A dummy `Sleeper` impl that prints then becomes ready!
//! struct DummySleeper;
//!
//! impl Sleeper for DummySleeper {
//! type Sleep = std::future::Ready<()>;
//!
//! fn sleep(&self, dur: Duration) -> Self::Sleep {
//! println!("Hello from DummySleeper!");
//! std::future::ready(())
//! }
//! }
//! ```
//!
//! ## The empty Sleeper
//!
//! If neither feature is enabled nor a custom implementation is provided, BackON
//! will fallback to the empty sleeper, in which case, a compile-time error that
//! `PleaseEnableAFeatureOrProvideACustomSleeper needs to implement Sleeper or
//! BlockingSleeper` will be raised to remind you to choose or bring a real Sleeper
//! implementation.
//!
//! # Retry
//!
Expand Down

0 comments on commit b3ca11e

Please sign in to comment.