1 stable release
Uses new Rust 2024
| 1.0.0 | Oct 18, 2025 |
|---|
#1863 in Procedural macros
24KB
453 lines
backon-macros

Attribute macros that make backon retry APIs feel like native syntax.
- Fluent ergonomics: Mark a function with
#[backon]and reuse the builder-style retry API without writing closures. - Async & sync aware: The macro inspects your signature to choose between
RetryableandBlockingRetryable. - Configurable: Opt into custom backoff strategies, sleepers, filters, notifications, or context capture with simple arguments.
Installation
Add both backon and backon-macros to your Cargo.toml:
[dependencies]
backon = "1"
backon-macros = "1"
Usage
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())
}
Parameters
| 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 mode
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.
Examples
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.
Limitations
- Methods that take
&mut selfor ownselfare currently rejected with a compile-time error; write manual retry loops for now. - The macro does not support arbitrary patterns in parameter position—each argument must bind to an identifier.
- The generated code depends on the
backoncrate at runtime; ensure both crates are included in your workspace.
License
Licensed under Apache License, Version 2.0.
Dependencies
~120–490KB
~12K SLoC