5 releases
Uses new Rust 2024
new 0.0.5 | May 24, 2025 |
---|---|
0.0.4 | May 22, 2025 |
0.0.3 | May 22, 2025 |
0.0.2 | May 20, 2025 |
0.0.1 | May 20, 2025 |
#278 in Configuration
247 downloads per month
17KB
251 lines
🔄 backoff-config
⚙️
backoff-config
makes configuring backoff
more flexible by providing a unified Backoff
strategy enum and a
BackoffConfig
that supports deserialization.
The actual backoff logic is powered by the awesome backon crate. Make sure to check out backon to explore its amazing features and ergonomics!
backoff-config
integrates with backon by implementing:
Usage
- Add
backoff-config
to your dependencies:
cargo add backoff-config
- Load
BackoffConfig
and use it straight away in retries. Example with figment:
use serde::Deserialize;
use backon::Retryable;
#[derive(Deserialize)]
pub struct Config {
pub backoff: BackoffConfig,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Env variables:
//
// CONFIG__BACKOFF__STRATEGY=Constant
// CONFIG__BACKOFF__DELAY=1s
// CONFIG__BACKOFF__MAX_RETRIES=4
let config = figment::Figment::new()
.merge(Env::prefixed("CONFIG__").split("__"))
.extract::<Config>()?;
// Use in retries:
let response_body = fetch.retry(config.backoff).await?;
println!("Response body: {response_body}");
Ok(())
}
// Function that may fail
async fn fetch() -> anyhow::Result<String> {
let body = reqwest::get("https://www.rust-lang.org")
.await?
.text()
.await?;
Ok(body)
}
Examples
- TOML + figment:
cargo run --example toml
And some examples on data formats:
Dependencies
~2–2.6MB
~60K SLoC