Skip to main content

dex_sdk/
retry_policy.rs

1// Copyright (c) 2026 Super Durable, Inc.
2//
3// Licensed under the Super Durable Source License 1.0.
4// You may not use this file except in compliance with the License.
5// See the LICENSE file in the repository root.
6//
7// SPDX-License-Identifier: LicenseRef-Super-Durable-1.0
8
9use std::time::Duration;
10
11#[derive(Clone, Debug, Default, PartialEq)]
12/// Overrides Dex retry timing for a Flow, `wait_for`, or `execute` operation.
13///
14/// Unset fields use server defaults. Intervals and total duration use [`Duration`]; maximum
15/// attempts counts the initial attempt. A zero maximum attempts value uses the server behavior.
16///
17/// # Examples
18///
19/// ```
20/// use dex_sdk::RetryPolicy;
21/// use std::time::Duration;
22///
23/// let retry = RetryPolicy::new()
24///     .initial_interval(Duration::from_secs(1))
25///     .backoff_coefficient(2.0)
26///     .maximum_interval(Duration::from_secs(30))
27///     .maximum_attempts(5);
28/// ```
29pub struct RetryPolicy {
30    pub(crate) initial_interval: Option<Duration>,
31    pub(crate) backoff_coefficient: Option<f64>,
32    pub(crate) maximum_interval: Option<Duration>,
33    pub(crate) maximum_attempts: Option<u32>,
34    pub(crate) total_duration: Option<Duration>,
35}
36
37impl RetryPolicy {
38    /// Creates an empty policy that preserves every server default.
39    pub fn new() -> Self {
40        Self::default()
41    }
42
43    /// Sets the delay before the first retry.
44    pub fn initial_interval(mut self, value: Duration) -> Self {
45        self.initial_interval = Some(value);
46        self
47    }
48
49    /// Sets the multiplier applied to each successive retry interval.
50    pub fn backoff_coefficient(mut self, value: f64) -> Self {
51        self.backoff_coefficient = Some(value);
52        self
53    }
54
55    /// Caps the delay between retries.
56    pub fn maximum_interval(mut self, value: Duration) -> Self {
57        self.maximum_interval = Some(value);
58        self
59    }
60
61    /// Sets the total attempt limit, including the initial attempt.
62    pub fn maximum_attempts(mut self, value: u32) -> Self {
63        self.maximum_attempts = Some(value);
64        self
65    }
66
67    /// Sets the total elapsed-time limit for all attempts.
68    pub fn total_duration(mut self, value: Duration) -> Self {
69        self.total_duration = Some(value);
70        self
71    }
72}