restate_sdk/context/
run.rs1use crate::errors::HandlerResult;
2use crate::serde::{Deserialize, Serialize};
3use std::future::Future;
4use std::time::Duration;
5
6pub trait RunClosure {
8 type Output: Deserialize + Serialize + 'static;
9 type Fut: Future<Output = HandlerResult<Self::Output>>;
10
11 fn run(self) -> Self::Fut;
12}
13
14impl<F, O, Fut> RunClosure for F
15where
16 F: FnOnce() -> Fut,
17 Fut: Future<Output = HandlerResult<O>>,
18 O: Deserialize + Serialize + 'static,
19{
20 type Output = O;
21 type Fut = Fut;
22
23 fn run(self) -> Self::Fut {
24 self()
25 }
26}
27
28pub trait RunFuture<O>: Future<Output = O> {
30 fn retry_policy(self, retry_policy: RunRetryPolicy) -> Self;
35
36 fn name(self, name: impl Into<String>) -> Self;
40}
41
42#[derive(Debug, Clone)]
44pub struct RunRetryPolicy {
45 pub(crate) initial_delay: Duration,
46 pub(crate) factor: f32,
47 pub(crate) max_delay: Option<Duration>,
48 pub(crate) max_attempts: Option<u32>,
49 pub(crate) max_duration: Option<Duration>,
50}
51
52impl Default for RunRetryPolicy {
53 fn default() -> Self {
54 Self {
55 initial_delay: Duration::from_millis(100),
56 factor: 2.0,
57 max_delay: Some(Duration::from_secs(2)),
58 max_attempts: None,
59 max_duration: Some(Duration::from_secs(50)),
60 }
61 }
62}
63
64impl RunRetryPolicy {
65 pub fn new() -> Self {
67 Self {
68 initial_delay: Duration::from_millis(100),
69 factor: 1.0,
70 max_delay: None,
71 max_attempts: None,
72 max_duration: None,
73 }
74 }
75
76 pub fn initial_delay(mut self, initial_interval: Duration) -> Self {
78 self.initial_delay = initial_interval;
79 self
80 }
81
82 pub fn exponentiation_factor(mut self, factor: f32) -> Self {
84 self.factor = factor;
85 self
86 }
87
88 pub fn max_delay(mut self, max_interval: Duration) -> Self {
90 self.max_delay = Some(max_interval);
91 self
92 }
93
94 pub fn max_attempts(mut self, max_attempts: u32) -> Self {
102 self.max_attempts = Some(max_attempts);
103 self
104 }
105
106 pub fn max_duration(mut self, max_duration: Duration) -> Self {
114 self.max_duration = Some(max_duration);
115 self
116 }
117}