appletheia_application/outbox/
outbox_poll_interval.rs1use chrono::Duration;
2
3use super::{OutboxPollBackoffMultiplier, OutboxPollJitterRatio};
4
5#[derive(Copy, Clone, Debug, PartialEq)]
6pub struct OutboxPollInterval(Duration);
7
8impl OutboxPollInterval {
9 pub fn new(value: Duration) -> Self {
10 Self(value)
11 }
12
13 pub fn value(&self) -> Duration {
14 self.0
15 }
16
17 pub fn next(
18 self,
19 multiplier: OutboxPollBackoffMultiplier,
20 jitter: OutboxPollJitterRatio,
21 max: OutboxPollInterval,
22 ) -> Self {
23 let current = self.value();
24 let mut next_ms = current.num_milliseconds() as f64;
25 let max_ms = max.value().num_milliseconds() as f64;
26
27 let m = multiplier.value();
28 next_ms = (next_ms * m).max(0.0);
29
30 let jitter_ratio = jitter.value();
31 if jitter_ratio > 0.0 {
32 let jitter_factor = 1.0 + jitter_ratio;
33 next_ms *= jitter_factor;
34 }
35
36 if next_ms > max_ms {
37 next_ms = max_ms;
38 }
39
40 let clamped_ms = next_ms.round() as i64;
41 let next_duration = Duration::milliseconds(clamped_ms.max(0));
42
43 OutboxPollInterval::new(next_duration)
44 }
45}
46
47impl From<Duration> for OutboxPollInterval {
48 fn from(value: Duration) -> Self {
49 OutboxPollInterval::new(value)
50 }
51}
52
53impl From<OutboxPollInterval> for Duration {
54 fn from(value: OutboxPollInterval) -> Self {
55 value.value()
56 }
57}