aws_smithy_runtime/client/waiters/
backoff.rs

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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use std::{fmt, time::Duration};

#[derive(Debug)]
pub(super) struct Backoff {
    min_delay: Duration,
    max_delay: Duration,
    max_wait: Duration,
    attempt_ceiling: u32,
    random: RandomImpl,
}

impl Backoff {
    pub(super) fn new(
        min_delay: Duration,
        max_delay: Duration,
        max_wait: Duration,
        random: RandomImpl,
    ) -> Self {
        Self {
            min_delay,
            max_delay,
            max_wait,
            // Attempt ceiling calculation taken from the Smithy spec: https://smithy.io/2.0/additional-specs/waiters.html#waiter-retries
            attempt_ceiling: (((max_delay.as_secs_f64() / min_delay.as_secs_f64()).ln()
                / 2f64.ln())
                + 1.0) as u32,
            random,
        }
    }

    // Calculates backoff delay time according to the Smithy spec: https://smithy.io/2.0/additional-specs/waiters.html#waiter-retries
    pub(super) fn delay(&self, attempt: u32, elapsed: Duration) -> Duration {
        let delay = if attempt > self.attempt_ceiling {
            self.max_delay.as_secs()
        } else {
            self.min_delay.as_secs() * 2u64.pow(attempt - 1)
        };
        let mut delay = Duration::from_secs(self.random.random(self.min_delay.as_secs(), delay));

        let remaining_time = self.max_wait.saturating_sub(elapsed);
        if remaining_time.saturating_sub(delay) <= self.min_delay {
            // Note: deviating from the spec here. Subtracting `min_delay` doesn't fulfill the original intent.
            delay = remaining_time;
        }
        delay
    }

    #[inline]
    pub(super) fn max_wait(&self) -> Duration {
        self.max_wait
    }
}

#[derive(Default)]
pub(super) enum RandomImpl {
    #[default]
    Default,
    #[cfg(test)]
    Override(Box<dyn Fn(u64, u64) -> u64 + Send + Sync>),
}

impl fmt::Debug for RandomImpl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Default => write!(f, "Default"),
            #[cfg(test)]
            Self::Override(_) => f.debug_tuple("Override").field(&"** function **").finish(),
        }
    }
}

impl RandomImpl {
    fn random(&self, min_inclusive: u64, max_inclusive: u64) -> u64 {
        match self {
            Self::Default => fastrand::u64(min_inclusive..=max_inclusive),
            #[cfg(test)]
            Self::Override(overrid) => (overrid)(min_inclusive, max_inclusive),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::{Arc, Mutex};

    fn test_backoff(
        min_delay: u64,
        max_delay: u64,
        test_random: impl Fn(u64, u64) -> u64 + Send + Sync + 'static,
        attempt_delays: &[(u64, u64)],
    ) {
        let backoff = dbg!(Backoff::new(
            Duration::from_secs(min_delay),
            Duration::from_secs(max_delay),
            Duration::from_secs(300),
            RandomImpl::Override(Box::new(test_random)),
        ));

        for (index, (delay, time)) in attempt_delays.iter().enumerate() {
            let attempt = index + 1;
            println!("attempt: {attempt}, delay: {delay}, time: {time}");
            assert_eq!(
                Duration::from_secs(*delay),
                backoff.delay(attempt as _, Duration::from_secs(*time))
            );
        }
    }

    #[test]
    fn backoff_jitter_as_average() {
        let test_random = |min: u64, max: u64| (min + max) / 2;
        let attempt_delays = &[
            // delay, time
            (2, 2),
            (3, 4),
            (5, 7),
            (9, 12),
            (17, 21),
            (33, 38),
            (61, 71),
            (61, 132),
            (61, 193),
            (46, 254),
            (0, 300),
        ];
        test_backoff(2, 120, test_random, attempt_delays);
    }

    #[test]
    fn backoff_with_seeded_jitter() {
        let random = Arc::new(Mutex::new(fastrand::Rng::with_seed(1)));
        let test_random = move |min: u64, max: u64| random.lock().unwrap().u64(min..=max);
        let attempt_delays = &[
            // delay, time
            (2, 2),
            (3, 4),
            (5, 7),
            (2, 12),
            (2, 14),
            (57, 16),
            (91, 73),
            (6, 164),
            (57, 170),
            (29, 227),
            (27, 256),
            (17, 283),
            (0, 300),
        ];
        test_backoff(2, 120, test_random, attempt_delays);
    }

    #[test]
    fn backoff_with_large_min_delay() {
        let test_random = |min: u64, max: u64| (min + max) / 2;
        let attempt_delays = &[
            // delay, time
            (15, 1),
            (22, 16),
            (37, 38),
            (67, 75),
            (67, 142),
            (24, 276),
            (0, 300),
        ];
        test_backoff(15, 120, test_random, attempt_delays);
    }
}