-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xuanwo <github@xuanwo.io>
- Loading branch information
Showing
19 changed files
with
260 additions
and
190 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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
edition = "2021" | ||
reorder_imports = true | ||
|
||
# format_code_in_doc_comments = true | ||
# imports_granularity = "Item" | ||
# group_imports = "StdExternalCrate" |
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,19 @@ | ||
Retry an async function. | ||
|
||
```rust | ||
use backon::ExponentialBuilder; | ||
use backon::Retryable; | ||
use anyhow::Result; | ||
|
||
async fn fetch() -> Result<String> { | ||
Ok("Hello, Workd!".to_string()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let content = fetch.retry(ExponentialBuilder::default()).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
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,22 @@ | ||
//! Examples of using backon. | ||
#[doc = include_str!("basic.md")] | ||
pub mod basic {} | ||
|
||
#[doc = include_str!("closure.md")] | ||
pub mod closure {} | ||
|
||
#[doc = include_str!("sqlx.md")] | ||
pub mod sqlx {} | ||
|
||
#[doc = include_str!("with_args.md")] | ||
pub mod with_args {} | ||
|
||
#[doc = include_str!("with_mut_self.md")] | ||
pub mod with_mut_self {} | ||
|
||
#[doc = include_str!("with_self.md")] | ||
pub mod with_self {} | ||
|
||
#[doc = include_str!("with_specific_error.md")] | ||
pub mod with_specific_error {} |
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 sqlx operations. | ||
|
||
```rust | ||
use backon::Retryable; | ||
use anyhow::Result; | ||
use backon::ExponentialBuilder; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let pool = sqlx::sqlite::SqlitePoolOptions::new() | ||
.max_connections(5) | ||
.connect("sqlite::memory:") | ||
.await?; | ||
|
||
let row: (i64,) = (|| sqlx::query_as("SELECT $1").bind(150_i64).fetch_one(&pool)) | ||
.retry(ExponentialBuilder::default()) | ||
.await?; | ||
|
||
assert_eq!(row.0, 150); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Retry function with args. | ||
|
||
It's a pity that rust doesn't allow us to implement `Retryable` for async function with args. So we have to use a workaround to make it work. | ||
|
||
```rust | ||
use anyhow::Result; | ||
use backon::ExponentialBuilder; | ||
use backon::Retryable; | ||
|
||
async fn fetch(url: &str) -> Result<String> { | ||
Ok(reqwest::get(url).await?.text().await?) | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<()> { | ||
let content = (|| async { fetch("https://www.rust-lang.org").await }) | ||
.retry(ExponentialBuilder::default()) | ||
.when(|e| e.to_string() == "retryable") | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Retry an async function which takes `&mut self` as receiver. | ||
|
||
This is a bit more complex since we need to capture the receiver in the closure with ownership. backon supports this use case by `RetryableWithContext`. | ||
|
||
```rust | ||
use anyhow::Result; | ||
use backon::ExponentialBuilder; | ||
use backon::RetryableWithContext; | ||
|
||
struct Test; | ||
|
||
impl Test { | ||
async fn fetch(&mut self, url: &str) -> Result<String> { | ||
Ok(reqwest::get(url).await?.text().await?) | ||
} | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<()> { | ||
let test = Test; | ||
|
||
let (_, result) = (|mut v: Test| async { | ||
let res = v.fetch("https://www.rust-lang.org").await; | ||
// Return input context back. | ||
(v, res) | ||
}) | ||
.retry(ExponentialBuilder::default()) | ||
// Passing context in. | ||
.context(test) | ||
.when(|e| e.to_string() == "retryable") | ||
.await; | ||
|
||
println!("fetch succeeded: {}", result.unwrap()); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
Retry an async function which takes `&self` as receiver. | ||
|
||
```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?) | ||
} | ||
} | ||
|
||
#[tokio::main(flavor = "current_thread")] | ||
async fn main() -> Result<()> { | ||
let test = Test; | ||
let content = (|| async { test.fetch("https://www.rust-lang.org").await }) | ||
.retry(ExponentialBuilder::default()) | ||
.when(|e| e.to_string() == "retryable") | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Retry with specify retryable error by `when`. | ||
|
||
```rust | ||
use anyhow::Result; | ||
use backon::ExponentialBuilder; | ||
use backon::Retryable; | ||
|
||
async fn fetch() -> Result<String> { | ||
Ok("Hello, Workd!".to_string()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let content = fetch | ||
.retry(ExponentialBuilder::default()) | ||
.when(|e| e.to_string() == "retryable") | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! Docs for the backon crate, like examples. | ||
pub mod examples; |
Oops, something went wrong.