Skip to content

Commit

Permalink
Version bump and retry strategy example
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Jun 4, 2023
1 parent 461486f commit 29fc713
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lastfm"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
authors = ["Luciano Mammino"]
description = "An async client to fetch your Last.fm listening history or the track you are currently playing"
Expand Down
34 changes: 34 additions & 0 deletions examples/customise_retry_strategy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use lastfm::{retry_strategy::RetryStrategy, ClientBuilder};
use std::time::Duration;

/// A retry strategy that will retry 3 times with the following delays:
/// - Retry 0: 0 second delay
/// - Retry 1: 1 second delay
/// - Retry 2: 2 seconds delay
struct SimpleRetryStrategy {}

impl RetryStrategy for SimpleRetryStrategy {
fn should_retry_after(&self, attempt: usize) -> Option<std::time::Duration> {
// if retrying more than 3 times stop
if attempt >= 3 {
return None;
}

// otherwise wait a second per number of attempts
Some(Duration::from_secs(attempt as u64))
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let retry_strategy = SimpleRetryStrategy {};

let client = ClientBuilder::new("some-api-key", "loige")
.retry_strategy(Box::from(retry_strategy))
.build();

// do something with client...
dbg!(client);

Ok(())
}

0 comments on commit 29fc713

Please sign in to comment.