From 29fc71363e45c452397ce317c5b7f305b46f425b Mon Sep 17 00:00:00 2001 From: Luciano Mammino Date: Sun, 4 Jun 2023 23:37:48 +0100 Subject: [PATCH] Version bump and retry strategy example --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/customise_retry_strategy.rs | 34 ++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 examples/customise_retry_strategy.rs diff --git a/Cargo.lock b/Cargo.lock index f54c5bb..63ea319 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -490,7 +490,7 @@ dependencies = [ [[package]] name = "lastfm" -version = "0.4.0" +version = "0.5.0" dependencies = [ "async-stream", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 77c6b65..b98e54c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/customise_retry_strategy.rs b/examples/customise_retry_strategy.rs new file mode 100644 index 0000000..5824a0a --- /dev/null +++ b/examples/customise_retry_strategy.rs @@ -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 { + // 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> { + 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(()) +}