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 derives a stable Request ID from the Step execution when none is supplied.
17/// `request_timeout` bounds the SDK call across transport reattachments.
18/// Temporal permits 10 in-flight Updates per Workflow Execution. `internal_handler_timeout`
19/// reclaims accepted waits that outlive callers and could consume those slots. Active callers
20/// transparently start another generation, which adds another Update to history.
21pub struct WaitForStepCompletionOptions {
22 pub(crate) request_id: String,
23 pub(crate) request_timeout: Duration,
24 pub(crate) internal_handler_timeout: Duration,
25}
26
27impl WaitForStepCompletionOptions {
28 /// Creates an infinite wait that uses the server-derived stable Request ID.
29 pub fn new() -> Self {
30 Self::default()
31 }
32
33 /// Overrides the stable Request ID derived from the Step execution.
34 pub fn request_id(mut self, request_id: impl Into<String>) -> Self {
35 self.request_id = request_id.into();
36 self
37 }
38
39 /// Sets the total SDK call budget. Zero waits indefinitely.
40 pub fn request_timeout(mut self, request_timeout: Duration) -> Self {
41 self.request_timeout = request_timeout;
42 self
43 }
44
45 /// Sets the Temporal Update handler generation lifetime.
46 ///
47 /// Use a positive value only when abandoned waits can approach Temporal's in-flight Update
48 /// limit. Active callers transparently start another generation. Zero disables rollover.
49 /// Prefer a value longer than normal request timeouts and reconnect gaps because every
50 /// generation counts toward Temporal's 2,000-Update history limit.
51 pub fn internal_handler_timeout(mut self, internal_handler_timeout: Duration) -> Self {
52 self.internal_handler_timeout = internal_handler_timeout;
53 self
54 }
55}
56
57#[derive(Clone, Debug, Default)]
58/// Configures one durable Attribute match wait.
59///
60/// The server derives a stable Request ID from the Attribute condition when none is supplied.
61/// `request_timeout` bounds the SDK call across transport reattachments.
62/// Temporal permits 10 in-flight Updates per Workflow Execution. `internal_handler_timeout`
63/// reclaims accepted waits that outlive callers and could consume those slots. Active callers
64/// transparently start another generation, which adds another Update to history.
65pub struct WaitForAttributeOptions {
66 pub(crate) request_id: String,
67 pub(crate) request_timeout: Duration,
68 pub(crate) internal_handler_timeout: Duration,
69}
70
71impl WaitForAttributeOptions {
72 /// Creates an infinite wait that uses the server-derived stable Request ID.
73 pub fn new() -> Self {
74 Self::default()
75 }
76
77 /// Overrides the stable Request ID derived from the Attribute condition.
78 pub fn request_id(mut self, request_id: impl Into<String>) -> Self {
79 self.request_id = request_id.into();
80 self
81 }
82
83 /// Sets the total SDK call budget. Zero waits indefinitely.
84 pub fn request_timeout(mut self, request_timeout: Duration) -> Self {
85 self.request_timeout = request_timeout;
86 self
87 }
88
89 /// Sets the Temporal Update handler generation lifetime.
90 ///
91 /// Use a positive value only when abandoned waits can approach Temporal's in-flight Update
92 /// limit. Active callers transparently start another generation. Zero disables rollover.
93 /// Prefer a value longer than normal request timeouts and reconnect gaps because every
94 /// generation counts toward Temporal's 2,000-Update history limit.
95 pub fn internal_handler_timeout(mut self, internal_handler_timeout: Duration) -> Self {
96 self.internal_handler_timeout = internal_handler_timeout;
97 self
98 }
99}
100
101pub(crate) struct ClientRequestAttempt {
102 pub(crate) request_timeout_seconds: i32,
103 pub(crate) transport_timeout: Option<Duration>,
104}
105
106pub(crate) struct ClientRequestBudget {
107 deadline: Option<Instant>,
108}
109
110impl ClientRequestBudget {
111 pub(crate) fn new(request_timeout: Duration) -> SdkResult<Self> {
112 crate::client::seconds32(request_timeout)?;
113 Ok(Self {
114 deadline: (!request_timeout.is_zero()).then(|| Instant::now() + request_timeout),
115 })
116 }
117
118 pub(crate) fn next_attempt(
119 &self,
120 operation: &'static str,
121 flow_id: &str,
122 ) -> SdkResult<ClientRequestAttempt> {
123 let Some(deadline) = self.deadline else {
124 return Ok(ClientRequestAttempt {
125 request_timeout_seconds: 0,
126 transport_timeout: None,
127 });
128 };
129 let remaining = deadline.saturating_duration_since(Instant::now());
130 if remaining.is_zero() {
131 return Err(SdkError::request_timeout(operation, flow_id));
132 }
133 let seconds = remaining.as_secs() + u64::from(remaining.subsec_nanos() > 0);
134 Ok(ClientRequestAttempt {
135 request_timeout_seconds: i32::try_from(seconds)
136 .map_err(|_| crate::client::invalid("Duration exceeds int32"))?,
137 transport_timeout: Some(remaining),
138 })
139 }
140
141 pub(crate) fn has_deadline(&self) -> bool {
142 self.deadline.is_some()
143 }
144}