| Crates.io | backon-macros |
| lib.rs | backon-macros |
| version | 1.0.0 |
| created_at | 2025-10-18 17:49:57.194525+00 |
| updated_at | 2025-10-18 17:49:57.194525+00 |
| description | Attribute macro helpers for the backon retry library. |
| homepage | |
| repository | https://github.com/Xuanwo/backon |
| max_upload_size | |
| id | 1889451 |
| size | 37,094 |
Attribute macros that make backon retry APIs feel like native syntax.
#[backon] and reuse the builder-style retry API without writing closures.Retryable and BlockingRetryable.Add both backon and backon-macros to your Cargo.toml:
[dependencies]
backon = "1"
backon-macros = "1"
Annotate any free function or inherent method (taking &self) with #[backon]. The macro rewrites the body so it runs inside a retry loop using the backon primitives.
use anyhow::Result;
use backon::TokioSleeper;
use backon_macros::backon;
#[backon(
backoff = backon::ExponentialBuilder::default,
sleep = TokioSleeper::sleep,
when = |e: &anyhow::Error| e.to_string() == "temporary",
notify = |err: &anyhow::Error, dur| tracing::warn!(?err, ?dur, "retrying")
)]
async fn fetch_data() -> Result<String> {
Ok("value".to_string())
}
| Argument | Description |
|---|---|
backoff = path |
Builder function that returns the backoff strategy. Defaults to backon::ExponentialBuilder::default. |
sleep = path |
Sleeper implementation for async or blocking retries. |
when = path |
Predicate used to decide if an error should trigger another attempt. |
notify = path |
Callback invoked before sleeping. |
adjust = path |
Async-only hook that can modify the next delay. |
context = true |
Capture arguments into a context tuple for RetryableWithContext. Use when the closure would otherwise borrow values that cannot cross await points. |
context = true is available for free functions and methods without receivers, and for methods that take ownership of their receiver. It cannot be combined with &self or &mut self methods; those cases must still use manual RetryableWithContext until dedicated support is added.
Try the ready-to-run programs under examples/ with cargo run --example ... for async, blocking, and method-based workflows. For more exhaustive coverage, check the trybuild tests.
&mut self or own self are currently rejected with a compile-time error; write manual retry loops for now.backon crate at runtime; ensure both crates are included in your workspace.Licensed under Apache License, Version 2.0.