Skip to main content

Idempotent

Trait Idempotent 

Source
pub trait Idempotent { }
Expand description

Marker trait for request types that are safe to retry.

A request is idempotent when repeating it produces the same observable side-effects as issuing it once (RFC 9110 §9.2.2). Typical examples: GET, HEAD, PUT, DELETE.

Implement this trait on your request structs to opt into generic retry helpers that gate retries on idempotency:

use api_bones::retry::{Idempotent, RetryPolicy};
use core::time::Duration;

struct DeleteResource { id: u64 }
impl Idempotent for DeleteResource {}

fn maybe_retry<R: Idempotent>(policy: &RetryPolicy, attempt: u32) -> Option<Duration> {
    if attempt < policy.max_attempts {
        Some(policy.next_delay(attempt))
    } else {
        None
    }
}

let policy = RetryPolicy::fixed(3, Duration::from_millis(500));
let req = DeleteResource { id: 42 };
let delay = maybe_retry::<DeleteResource>(&policy, 0);
assert_eq!(delay, Some(Duration::from_millis(500)));

Implementors§