Expand description
Attribute macros that integrate with the backon retry library.
§Overview
This crate provides the #[backon] attribute for free functions and inherent
methods. Annotated items are rewritten so their bodies execute inside the
backon retry pipeline, matching the fluent builder style from the runtime
crate without hand-written closures.
The macro inspects the target signature to decide whether to call
Retryable or BlockingRetryable.
When context = true is supplied, it switches to the corresponding *_WithContext
traits so the arguments are preserved across retries.
§Usage
use std::time::Duration;
use backon_macros::backon;
#[derive(Debug)]
enum ExampleError {
Temporary,
Fatal,
}
fn should_retry(err: &ExampleError) -> bool {
matches!(err, ExampleError::Temporary)
}
fn log_retry(err: &ExampleError, dur: Duration) {
println!("retrying after {dur:?}: {err:?}");
}
#[backon(
backoff = backon::ExponentialBuilder::default,
sleep = tokio::time::sleep,
when = should_retry,
notify = log_retry
)]
async fn fetch() -> Result<String, ExampleError> {
Ok("value".to_string())
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), ExampleError> {
let value = fetch().await?;
println!("{value}");
Ok(())
}§Parameters
backoff = path– Builder that creates a backoff strategy. Defaults tobackon::ExponentialBuilder::default.sleep = path– Sleeper function used for async or blocking retries.when = path– Predicate that filters retryable errors.notify = path– Callback invoked before each sleep.adjust = path– Async-only hook that can override the delay.context = true– Capture inputs into a context tuple and use theRetryableWithContexttraits.
§Limitations
- Methods that take
&mut selfor ownselfare not generated; fallback to manualRetryableWithContextuntil support lands. - Parameters must bind to identifiers; destructuring patterns are rejected.
context = trueis unavailable for&selfmethods.
Attribute Macros§
- backon
- Attribute for turning a function into a retried one using backon retry APIs.