pub struct RetryPolicy {
pub max_retries: u32,
pub initial_backoff: Duration,
pub max_backoff: Duration,
pub jitter_percent: u32,
pub retry_non_idempotent: bool,
}client only.Expand description
When to try again, and how long to wait.
§The rule that matters
Retrying an OICP call is not always safe, and the difference is not the HTTP status:
- A transport failure may mean the request never arrived, or that it arrived and the answer
was lost. For an
AuthorizeRemoteStartthe second case means retrying could start a second charging session. So retries are allowed only where the operation is idempotent, or where Hubject’s own session handling makes a duplicate harmless. - A rejection (
HTTP 200,Result: false) is a decision, and repeating an identical request gets an identical decision — except for the handful of transient codes thatCode::is_retryablenames.
RetryPolicy::should_retry takes the operation as well as the error, so it can apply both.
let policy = RetryPolicy::default();
let lost = OicpError::transport("connection reset");
// A push is idempotent: send it again.
assert!(policy.should_retry(Operation::PushEvseData, &lost, 0).is_some());
// A remote start is not: a second one could start a second session.
assert!(policy.should_retry(Operation::AuthorizeRemoteStart, &lost, 0).is_none());Fields§
§max_retries: u32How many times to try again after the first attempt.
initial_backoff: DurationThe wait before the first retry.
max_backoff: DurationThe cap on the wait.
jitter_percent: u32How much of the backoff is random, as a percentage of it — 0 for none.
Without jitter, every client that lost the same Hubject instance retries in lockstep and arrives together, which is how a recovering service is knocked over again.
retry_non_idempotent: boolRetry operations that are not idempotent.
Off by default. Turn it on only if your own system deduplicates on the partner session id.
Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub const fn with_max_retries(self, max_retries: u32) -> Self
pub const fn with_max_retries(self, max_retries: u32) -> Self
Sets how many retries to allow.
Sourcepub const fn allowing_non_idempotent(self) -> Self
pub const fn allowing_non_idempotent(self) -> Self
Allows retrying operations that are not idempotent. See the field documentation.
Sourcepub fn should_retry(
&self,
operation: Operation,
error: &OicpError,
attempt: u32,
) -> Option<Duration>
pub fn should_retry( &self, operation: Operation, error: &OicpError, attempt: u32, ) -> Option<Duration>
How long to wait before attempt attempt + 1, or None to give up.
attempt counts from zero for the first failure.
Sourcepub fn backoff(&self, attempt: u32) -> Duration
pub fn backoff(&self, attempt: u32) -> Duration
The backoff for attempt, doubling and capped, before jitter.
Sourcepub fn jittered_backoff(&self, attempt: u32, position_permille: u32) -> Duration
pub fn jittered_backoff(&self, attempt: u32, position_permille: u32) -> Duration
The backoff for attempt with jitter applied, given a position in 0..=1000.
500 is the un-jittered backoff, 0 the earliest and 1000 the latest the spread
allows. Split out so the jitter is testable without a random source — and computed in
integer milliseconds, so the crate needs neither a floating-point multiply nor a
random-number dependency to spread its retries.