-
-
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: Add a new example for retry inside
&mut self
function (#118)
Signed-off-by: Xuanwo <github@xuanwo.io>
- Loading branch information
Showing
3 changed files
with
27 additions
and
0 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,23 @@ | ||
Retry an async function inside `&mut self` functions. | ||
|
||
```rust | ||
use anyhow::Result; | ||
use backon::ExponentialBuilder; | ||
use backon::Retryable; | ||
|
||
struct Test; | ||
|
||
impl Test { | ||
async fn fetch(&self, url: &str) -> Result<String> { | ||
Ok(reqwest::get(url).await?.text().await?) | ||
} | ||
|
||
async fn run(&mut self) -> Result<String> { | ||
let content = (|| async { self.fetch("https://www.rust-lang.org").await }) | ||
.retry(ExponentialBuilder::default()) | ||
.when(|e| e.to_string() == "retryable") | ||
.await?; | ||
Ok(content) | ||
} | ||
} | ||
``` |
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
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