Skip to content

Commit

Permalink
Add examples (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
balroggg authored Feb 22, 2023
1 parent a39d446 commit e1d25df
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use anyhow::Result;
use backon::ExponentialBuilder;
use backon::Retryable;

async fn fetch() -> Result<String> {
let response = reqwest::get("https://httpbingo.org/unstable?failure_rate=0.7").await?;
if !response.status().is_success() {
println!("{}", response.status());
anyhow::bail!("some kind of error");
}
let text = response.text().await?;
Ok(text)
}

#[tokio::main]
async fn main() -> Result<()> {
let _ = fetch.retry(&ExponentialBuilder::default()).await?;
println!("fetch succeeded");

Ok(())
}
14 changes: 14 additions & 0 deletions examples/blocking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use anyhow::Result;
use backon::BlockingRetryable;
use backon::ExponentialBuilder;

fn fetch() -> Result<String> {
Ok("hello, world!".to_string())
}

fn main() -> Result<()> {
let content = fetch.retry(&ExponentialBuilder::default()).call()?;
println!("fetch succeeded: {}", content);

Ok(())
}

0 comments on commit e1d25df

Please sign in to comment.