Skip to main content

miden_node_utils/
retry.rs

1//! Shared retry/backoff helpers built on top of the [`backon`] crate.
2//!
3//! These constructors give the node a single definition of the "standard" backoff schedules so
4//! that retry behaviour stays consistent across components instead of being re-derived at each call
5//! site. Use them together with the [`Retryable`] suffix extension, e.g.
6//!
7//! ```ignore
8//! use miden_node_utils::retry::{self, Retryable};
9//!
10//! let value = (|| async { do_thing().await })
11//!     .retry(retry::exponential(min, max))
12//!     .when(|err| is_transient(err))
13//!     .notify(|err, dur| tracing::warn!(?dur, %err, "retrying"))
14//!     .await?;
15//! ```
16
17use std::time::Duration;
18
19pub use backon::{BackoffBuilder, Retryable};
20use backon::{ConstantBuilder, ExponentialBuilder};
21
22// BACKOFF BUILDERS
23// ================================================================================================
24
25/// Builds an exponential backoff schedule that retries indefinitely.
26///
27/// Delays start at `min`, double on each attempt (factor `2.0`), are capped at `max`, and have
28/// jitter applied to spread out concurrent retriers.
29pub fn exponential(min: Duration, max: Duration) -> ExponentialBuilder {
30    ExponentialBuilder::default()
31        .with_min_delay(min)
32        .with_max_delay(max)
33        .with_factor(2.0)
34        .with_jitter()
35        .without_max_times()
36}
37
38/// Same as [`exponential`], but stops after `max_times` retries (i.e. `max_times + 1` total
39/// attempts).
40pub fn exponential_bounded(min: Duration, max: Duration, max_times: usize) -> ExponentialBuilder {
41    exponential(min, max).with_max_times(max_times)
42}
43
44/// Builds a constant-delay backoff schedule.
45///
46/// `max_times` bounds the number of retries; pass `None` to retry indefinitely.
47pub fn constant(delay: Duration, max_times: Option<usize>) -> ConstantBuilder {
48    let builder = ConstantBuilder::default().with_delay(delay);
49    match max_times {
50        Some(max_times) => builder.with_max_times(max_times),
51        None => builder.without_max_times(),
52    }
53}