Rust

retry

Module

Retrying transient failures: the Retry decorator and its Strategy.

Contents

use utopia_client::Backoff;
Rust
pub struct Backoff

The default, best-practice strategy: retry only idempotent methods (GET, HEAD, PUT, DELETE, OPTIONS, TRACE), only on network failures (Error::is_network) and overloaded responses (429, 502, 503, 504), with exponential backoff and full jitter. A numeric Retry-After is honoured, capped to the maximum delay.

Rust
use utopia_client::{Backoff, Error, Outcome, Request, Strategy};

// A randomizer that always returns 1.0 shows the ceiling of each delay.
let backoff = Backoff::new(4, 0.1, 10.0, 2.0).with_randomizer(|| 1.0);
let request = Request::new("GET", "https://example.com/")?;
let error = Error::Connection("reset".into());
assert_eq!(backoff.delay(&request, 1, Outcome::Error(&error)), Some(0.1));
assert_eq!(backoff.delay(&request, 2, Outcome::Error(&error)), Some(0.2));
assert_eq!(backoff.delay(&request, 4, Outcome::Error(&error)), None);
Also implements Debug, Default.
use utopia_client::Retry;
Rust
pub struct Retry<A, S = Backoff, W = Timer>

An adapter that retries another according to a Strategy. The strategy owns every decision (which outcomes to retry, how long to wait, when to stop); the decorator only loops and sleeps. A stream is retried only while no byte has reached its sink: replaying would duplicate them.

Rust
use utopia_client::{Backoff, Client, Http, Request, Retry};

let client = Client::new(Retry::new(Http::new(), Backoff::default()));
let response = client.send(Request::new("GET", "https://example.com/")?).await?;
Compiles in CI; needs a running service to execute.
use utopia_client::Timer;
Rust
pub struct Timer

Sleeps on tokio's timer.

use utopia_client::Outcome;
Rust
pub enum Outcome<'a>

What happened on one attempt: the response, or the error.

use utopia_client::Sleep;
Rust
pub trait Sleep: Send + Sync

Waits between attempts. The default sleeps on tokio's timer; give your own to record or skip waits.

Implemented byTimer
use utopia_client::Strategy;
Rust
pub trait Strategy: Send + Sync

Decides whether to retry after an attempt. Implement it for a policy of your own.

Implemented byBackoff