Skip to main content

dex_sdk/
wait_options.rs

1// Copyright (c) 2026 Super Durable, Inc.
2//
3// Licensed under the Sustainable Use 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-Sustainable-Use-1.0
8
9use std::time::{Duration, Instant};
10
11use crate::{SdkError, SdkResult};
12
13#[derive(Clone, Debug, Default)]
14/// Configures one durable Step completion wait.
15///
16/// The Server does not read the request ID for this wait.
17/// `request_timeout` bounds the SDK call across transport reattachments.
18pub struct WaitForStepCompletionOptions {
19    pub(crate) request_id: String,
20    pub(crate) request_timeout: Duration,
21}
22
23impl WaitForStepCompletionOptions {
24    /// Creates an infinite wait.
25    pub fn new() -> Self {
26        Self::default()
27    }
28
29    /// Sets an optional request ID; the Server does not read it.
30    pub fn request_id(mut self, request_id: impl Into<String>) -> Self {
31        self.request_id = request_id.into();
32        self
33    }
34
35    /// Sets the total SDK call budget. Zero waits indefinitely.
36    pub fn request_timeout(mut self, request_timeout: Duration) -> Self {
37        self.request_timeout = request_timeout;
38        self
39    }
40}
41
42#[derive(Clone, Debug, Default)]
43/// Configures one durable Attribute match wait.
44///
45/// The Server does not read the request ID for this wait.
46/// `request_timeout` bounds the SDK call across transport reattachments.
47pub struct WaitForAttributeOptions {
48    pub(crate) request_id: String,
49    pub(crate) request_timeout: Duration,
50}
51
52impl WaitForAttributeOptions {
53    /// Creates an infinite wait.
54    pub fn new() -> Self {
55        Self::default()
56    }
57
58    /// Sets an optional request ID; the Server does not read it.
59    pub fn request_id(mut self, request_id: impl Into<String>) -> Self {
60        self.request_id = request_id.into();
61        self
62    }
63
64    /// Sets the total SDK call budget. Zero waits indefinitely.
65    pub fn request_timeout(mut self, request_timeout: Duration) -> Self {
66        self.request_timeout = request_timeout;
67        self
68    }
69}
70
71pub(crate) struct ClientRequestAttempt {
72    pub(crate) request_timeout_seconds: i32,
73    pub(crate) transport_timeout: Option<Duration>,
74}
75
76pub(crate) struct ClientRequestBudget {
77    deadline: Option<Instant>,
78}
79
80impl ClientRequestBudget {
81    pub(crate) fn new(request_timeout: Duration) -> SdkResult<Self> {
82        crate::client::seconds32(request_timeout)?;
83        Ok(Self {
84            deadline: (!request_timeout.is_zero()).then(|| Instant::now() + request_timeout),
85        })
86    }
87
88    pub(crate) fn next_attempt(
89        &self,
90        operation: &'static str,
91        flow_id: &str,
92    ) -> SdkResult<ClientRequestAttempt> {
93        let Some(deadline) = self.deadline else {
94            return Ok(ClientRequestAttempt {
95                request_timeout_seconds: 0,
96                transport_timeout: None,
97            });
98        };
99        let remaining = deadline.saturating_duration_since(Instant::now());
100        if remaining.is_zero() {
101            return Err(SdkError::request_timeout(operation, flow_id));
102        }
103        let seconds = remaining.as_secs() + u64::from(remaining.subsec_nanos() > 0);
104        Ok(ClientRequestAttempt {
105            request_timeout_seconds: i32::try_from(seconds)
106                .map_err(|_| crate::client::invalid("Duration exceeds int32"))?,
107            transport_timeout: Some(remaining),
108        })
109    }
110
111    pub(crate) fn deadline(&self) -> Option<Instant> {
112        self.deadline
113    }
114
115    pub(crate) fn has_deadline(&self) -> bool {
116        self.deadline.is_some()
117    }
118}