1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2026 Super Durable, Inc.
//
// Licensed under the Sustainable Use License 1.0.
// You may not use this file except in compliance with the License.
// See the LICENSE file in the repository root.
//
// SPDX-License-Identifier: LicenseRef-Sustainable-Use-1.0
use std::time::{Duration, Instant};
use crate::{SdkError, SdkResult};
#[derive(Clone, Debug, Default)]
/// Configures one durable Step completion wait.
///
/// The server derives a stable Request ID from the Step execution when none is supplied.
/// An abandoned infinite wait remains in flight until completion or Flow closure.
pub struct WaitForStepCompletionOptions {
pub(crate) request_id: String,
pub(crate) maximum_wait_time: Duration,
}
impl WaitForStepCompletionOptions {
/// Creates an infinite wait that uses the server-derived stable Request ID.
pub fn new() -> Self {
Self::default()
}
/// Overrides the stable Request ID derived from the Step execution.
pub fn request_id(mut self, request_id: impl Into<String>) -> Self {
self.request_id = request_id.into();
self
}
/// Sets the total handler wait budget. Zero waits indefinitely.
pub fn maximum_wait_time(mut self, maximum_wait_time: Duration) -> Self {
self.maximum_wait_time = maximum_wait_time;
self
}
}
#[derive(Clone, Debug, Default)]
/// Configures one durable Attribute match wait.
///
/// The server derives a stable Request ID from the Attribute condition when none is supplied.
/// An abandoned infinite wait remains in flight until a match or Flow closure.
pub struct WaitForAttributeOptions {
pub(crate) request_id: String,
pub(crate) maximum_wait_time: Duration,
}
impl WaitForAttributeOptions {
/// Creates an infinite wait that uses the server-derived stable Request ID.
pub fn new() -> Self {
Self::default()
}
/// Overrides the stable Request ID derived from the Attribute condition.
pub fn request_id(mut self, request_id: impl Into<String>) -> Self {
self.request_id = request_id.into();
self
}
/// Sets the total handler wait budget. Zero waits indefinitely.
pub fn maximum_wait_time(mut self, maximum_wait_time: Duration) -> Self {
self.maximum_wait_time = maximum_wait_time;
self
}
}
pub(crate) struct ClientWaitBudget {
deadline: Option<Instant>,
}
impl ClientWaitBudget {
pub(crate) fn new(maximum_wait_time: Duration) -> SdkResult<Self> {
crate::client::seconds32(maximum_wait_time)?;
Ok(Self {
deadline: (!maximum_wait_time.is_zero()).then(|| Instant::now() + maximum_wait_time),
})
}
pub(crate) fn remaining_seconds(
&self,
operation: &'static str,
flow_id: &str,
) -> SdkResult<i32> {
let Some(deadline) = self.deadline else {
return Ok(0);
};
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return Err(SdkError::wait_handler_timeout(operation, flow_id));
}
let seconds = remaining.as_secs() + u64::from(remaining.subsec_nanos() > 0);
i32::try_from(seconds).map_err(|_| crate::client::invalid("Duration exceeds int32"))
}
}