pub struct RetryPolicy { /* private fields */ }Expand description
Configuration for client retry behavior.
§Examples
use std::time::Duration;
use openkind_client::RetryPolicy;
// Three quick retries, honoring the server's Retry-After.
let policy = RetryPolicy::new()
.max_retries(3)
.backoff_initial(Duration::from_millis(100))
.backoff_max(Duration::from_secs(2));Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub fn new() -> Self
pub fn new() -> Self
A policy with the SDK-default settings (same as RetryPolicy::default).
Sourcepub fn max_retries(self, max_retries: u32) -> Self
pub fn max_retries(self, max_retries: u32) -> Self
Maximum retries after the initial attempt; 0 disables retries.
Sourcepub fn backoff_initial(self, initial: Duration) -> Self
pub fn backoff_initial(self, initial: Duration) -> Self
First backoff delay, doubled each attempt up to backoff_max.
Zero disables backoff delays entirely.
Sourcepub fn backoff_max(self, max: Duration) -> Self
pub fn backoff_max(self, max: Duration) -> Self
Upper bound on exponential backoff delays. Zero disables backoff delays entirely.
Sourcepub fn backoff_jitter(self, jitter: f64) -> Self
pub fn backoff_jitter(self, jitter: f64) -> Self
Fraction of each backoff delay randomly subtracted, in 0.0..=1.0.
0.0 makes delays fully deterministic (useful in tests).
Sourcepub fn retry_status(self, status: u16) -> Self
pub fn retry_status(self, status: u16) -> Self
Retry these status codes (in addition to 5xx while
retry_server_errors is set). The
default retries 408 and 429.
Sourcepub fn with_retry_statuses(
self,
statuses: impl IntoIterator<Item = u16>,
) -> Self
pub fn with_retry_statuses( self, statuses: impl IntoIterator<Item = u16>, ) -> Self
Replace the retried-status list wholesale, mirroring the Python SDK’s
http_statuses=[...] semantics: only these codes (plus 5xx while
retry_server_errors is set) are retried.
RetryPolicy::new().with_retry_statuses([409]).retry_server_errors(false)
retries 409 and nothing else.
Sourcepub fn retry_predicate(self, predicate: Option<RetryPredicate>) -> Self
pub fn retry_predicate(self, predicate: Option<RetryPredicate>) -> Self
Opt extra errors into retrying, mirroring the Python SDK’s
predicate/exceptions options: Some(predicate) retries any error
for which the predicate returns true, even non-transient statuses
like 404. None (the default) uses only the built-in rules.
Sourcepub fn retry_server_errors(self, retry: bool) -> Self
pub fn retry_server_errors(self, retry: bool) -> Self
Whether any 5xx status (including 529 Overloaded) is retried. Default true.
Sourcepub fn respect_retry_after(self, respect: bool) -> Self
pub fn respect_retry_after(self, respect: bool) -> Self
Whether to honor Retry-After / retry-after-ms response headers
instead of the computed backoff. Default true.
Sourcepub fn retry_connection_errors(self, retry: bool) -> Self
pub fn retry_connection_errors(self, retry: bool) -> Self
Whether connection failures are retried. Default true.
Sourcepub fn retry_timeout_errors(self, retry: bool) -> Self
pub fn retry_timeout_errors(self, retry: bool) -> Self
Whether per-attempt timeouts are retried. Default true.
Sourcepub fn total_timeout(self, timeout: Option<Duration>) -> Self
pub fn total_timeout(self, timeout: Option<Duration>) -> Self
Total budget per call, including the initial attempt and all delays.
A retry whose delay would reach or exceed the remaining budget is not
attempted; the last error is returned instead. None disables the limit.
Sourcepub fn is_retryable(&self, error: &Error) -> bool
pub fn is_retryable(&self, error: &Error) -> bool
Whether this error should be retried under the policy.
Sourcepub fn delay_for(&self, error: &Error, retry_index: u32) -> Duration
pub fn delay_for(&self, error: &Error, retry_index: u32) -> Duration
How long to wait before the retry numbered retry_index
(0-based: 0 is the wait before the first retry).
A server-requested Retry-After takes precedence over computed
backoff when respect_retry_after is set.
Sourcepub fn backoff_delay(&self, retry_index: u32) -> Duration
pub fn backoff_delay(&self, retry_index: u32) -> Duration
Exponential backoff with subtractive jitter:
min(backoff_max, backoff_initial * 2^retry_index) * (1 - U * jitter).
Nanosecond precision throughout, so sub-millisecond initial delays and very large caps behave like the Python SDK’s float math.