Rust Tokio Timeout and Retry Backoff
Bound each async call with tokio timeout, retry transient failures with linear backoff, and return one typed exhaustion error.
Code
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;
use tokio::time::{sleep, timeout};
#[derive(Debug)]
enum FetchError {
Exhausted { attempts: u32, last: String },
}
async fn flaky_fetch(calls: &AtomicUsize) -> Result<String, String> {
let call = calls.fetch_add(1, Ordering::SeqCst) + 1;
match call {
1 | 2 => {
sleep(Duration::from_millis(80)).await;
Err(format!("upstream returned 503 on call {call}"))
}
3 => Ok("payload".to_string()),
other => Err(format!("unexpected call {other}")),
}
}
async fn retry_with_deadline(
calls: &AtomicUsize,
max_attempts: u32,
) -> Result<String, FetchError> {
let mut last = String::from("no attempt executed");
for attempt in 1..=max_attempts {
let budget = Duration::from_millis(120 + u64::from(attempt) * 40);
match timeout(budget, flaky_fetch(calls)).await {
Ok(Ok(payload)) => return Ok(payload),
Ok(Err(message)) => last = message,
Err(_) => last = format!("attempt {attempt} exceeded {budget:?}"),
}
if attempt < max_attempts {
let backoff = Duration::from_millis(10 * u64::from(attempt));
sleep(backoff).await;
}
}
Err(FetchError::Exhausted {
attempts: max_attempts,
last,
})
}
#[tokio::main]
async fn main() {
let calls = AtomicUsize::new(0);
match retry_with_deadline(&calls, 3).await {
Ok(payload) => println!(
"success after {} calls: {payload}",
calls.load(Ordering::SeqCst)
),
Err(FetchError::Exhausted { attempts, last }) => {
println!("gave up after {attempts} attempts: {last}");
}
}
}How the snippet works
Rust Tokio Timeout and Retry Backoff keeps everything in the functions flaky_fetch, retry_with_deadline and main.
for attempt in 1..=max_attempts is the loop that carries the per-item work. Fallible operations return Result, forcing the caller to handle both arms explicitly. Each possible shape of the input gets its own match arm, and the compiler checks the set is complete.
Points of care
- Plan for an install step:
tokio::time::is a real dependency, resolved by the package manager. - Errors travel through
Result; if you drop the?operators while adapting, each call site needs its ownmatch.
Using it
The main signature reads fn flaky_fetch(calls: &AtomicUsize) -> Result<String, String>
Output lines are prefixed (success after, gave up after), making each step’s effect easy to spot.
Treat main as the orchestrator calling retry_with_deadline in a fixed order.
Key statements
Err(FetchError::Exhausted { attempts, last }) => {
println!("gave up after {attempts} attempts: {last}");
}
match timeout(budget, flaky_fetch(calls)).await {
Ok(Ok(payload)) => return Ok(payload),
Ok(Err(message)) => last = message,
Err(_) => last = format!("attempt {attempt} exceeded {budget:?}"),
}
Err(format!("upstream returned 503 on call {call}"))
}
Interactive Tools for This Topic
FAQ
- Does the snippet panic on failure?
- As `Result` values — each fallible call returns one, and the caller matches or propagates it.
- Which packages must be installed first?
- Yes. The imports resolve to `tokio::time::`, which the package manager must provide.
Report a problem with this exampleOpens a pre-filled issue on GitHub.