-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: document custom Sleeper impl (#147)
### What does this PR do Adds an example for user-provided sleeper implementation. Closes #129
- Loading branch information
Showing
2 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters