|
| 1 | +use std::time::{Duration, Instant}; |
| 2 | + |
| 3 | +pub(crate) trait RetryStrategy { |
| 4 | + /// Return the next amount of time a failed request should delay before re-attempting. |
| 5 | + fn next_delay(&mut self, current_time: Instant) -> Duration; |
| 6 | + |
| 7 | + /// Modify the strategy's default base delay. |
| 8 | + fn change_base_delay(&mut self, base_delay: Duration); |
| 9 | + |
| 10 | + /// Used to indicate to the strategy that it can reset as a successful connection has been made. |
| 11 | + fn reset(&mut self, current_time: Instant); |
| 12 | +} |
| 13 | + |
| 14 | +const DEFAULT_RESET_RETRY_INTERVAL: Duration = Duration::from_secs(60); |
| 15 | + |
| 16 | +pub(crate) struct BackoffRetry { |
| 17 | + base_delay: Duration, |
| 18 | + max_delay: Duration, |
| 19 | + backoff_factor: u32, |
| 20 | + |
| 21 | + reset_interval: Duration, |
| 22 | + next_delay: Duration, |
| 23 | + good_since: Option<Instant>, |
| 24 | +} |
| 25 | + |
| 26 | +impl BackoffRetry { |
| 27 | + pub fn new(base_delay: Duration, max_delay: Duration, backoff_factor: u32) -> Self { |
| 28 | + Self { |
| 29 | + base_delay, |
| 30 | + max_delay, |
| 31 | + backoff_factor, |
| 32 | + reset_interval: DEFAULT_RESET_RETRY_INTERVAL, |
| 33 | + next_delay: base_delay, |
| 34 | + good_since: None, |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +impl RetryStrategy for BackoffRetry { |
| 40 | + fn next_delay(&mut self, current_time: Instant) -> Duration { |
| 41 | + let mut next_delay = self.next_delay; |
| 42 | + |
| 43 | + if let Some(good_since) = self.good_since { |
| 44 | + if current_time - good_since >= self.reset_interval { |
| 45 | + next_delay = self.base_delay; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + self.good_since = None; |
| 50 | + self.next_delay = std::cmp::min(self.max_delay, next_delay * self.backoff_factor); |
| 51 | + |
| 52 | + next_delay |
| 53 | + } |
| 54 | + |
| 55 | + fn change_base_delay(&mut self, base_delay: Duration) { |
| 56 | + self.base_delay = base_delay; |
| 57 | + self.next_delay = self.base_delay; |
| 58 | + } |
| 59 | + |
| 60 | + fn reset(&mut self, current_time: Instant) { |
| 61 | + // While the external application has indicated success, we don't actually want to reset the |
| 62 | + // retry policy just yet. Instead, we want to record the time it was successful. Then when |
| 63 | + // we calculate the next delay, we can reset the strategy ONLY when it has been at least |
| 64 | + // DEFAULT_RESET_RETRY_INTERVAL seconds. |
| 65 | + self.good_since = Some(current_time); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use std::ops::Add; |
| 72 | + use std::time::{Duration, Instant}; |
| 73 | + |
| 74 | + use crate::retry::{BackoffRetry, RetryStrategy}; |
| 75 | + |
| 76 | + #[test] |
| 77 | + fn test_fixed_retry() { |
| 78 | + let base = Duration::from_secs(10); |
| 79 | + let mut retry = BackoffRetry::new(base, Duration::from_secs(30), 1); |
| 80 | + let start = Instant::now() - Duration::from_secs(60); |
| 81 | + |
| 82 | + assert_eq!(retry.next_delay(start), base); |
| 83 | + assert_eq!(retry.next_delay(start.add(Duration::from_secs(1))), base); |
| 84 | + assert_eq!(retry.next_delay(start.add(Duration::from_secs(2))), base); |
| 85 | + } |
| 86 | + |
| 87 | + #[test] |
| 88 | + fn test_able_to_reset_base_delay() { |
| 89 | + let base = Duration::from_secs(10); |
| 90 | + let mut retry = BackoffRetry::new(base, Duration::from_secs(30), 1); |
| 91 | + let start = Instant::now(); |
| 92 | + |
| 93 | + assert_eq!(retry.next_delay(start), base); |
| 94 | + assert_eq!(retry.next_delay(start.add(Duration::from_secs(1))), base); |
| 95 | + |
| 96 | + let base = Duration::from_secs(3); |
| 97 | + retry.change_base_delay(base); |
| 98 | + assert_eq!(retry.next_delay(start.add(Duration::from_secs(2))), base); |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn test_with_backoff() { |
| 103 | + let base = Duration::from_secs(10); |
| 104 | + let max = Duration::from_secs(60); |
| 105 | + let mut retry = BackoffRetry::new(base, max, 2); |
| 106 | + let start = Instant::now() - Duration::from_secs(60); |
| 107 | + |
| 108 | + assert_eq!(retry.next_delay(start), base); |
| 109 | + assert_eq!( |
| 110 | + retry.next_delay(start.add(Duration::from_secs(1))), |
| 111 | + base * 2 |
| 112 | + ); |
| 113 | + assert_eq!( |
| 114 | + retry.next_delay(start.add(Duration::from_secs(2))), |
| 115 | + base * 4 |
| 116 | + ); |
| 117 | + assert_eq!(retry.next_delay(start.add(Duration::from_secs(3))), max); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_retry_holds_at_max() { |
| 122 | + let base = Duration::from_secs(20); |
| 123 | + let max = Duration::from_secs(30); |
| 124 | + |
| 125 | + let mut retry = BackoffRetry::new(base, max, 2); |
| 126 | + let start = Instant::now(); |
| 127 | + retry.reset(start); |
| 128 | + |
| 129 | + let time = start.add(Duration::from_secs(20)); |
| 130 | + let delay = retry.next_delay(time); |
| 131 | + assert_eq!(delay, base); |
| 132 | + |
| 133 | + let time = time.add(Duration::from_secs(20)); |
| 134 | + let delay = retry.next_delay(time); |
| 135 | + assert_eq!(delay, max); |
| 136 | + |
| 137 | + let time = time.add(Duration::from_secs(20)); |
| 138 | + let delay = retry.next_delay(time); |
| 139 | + assert_eq!(delay, max); |
| 140 | + } |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn test_reset_interval() { |
| 144 | + let base = Duration::from_secs(10); |
| 145 | + let max = Duration::from_secs(60); |
| 146 | + let reset_interval = Duration::from_secs(45); |
| 147 | + |
| 148 | + // Prepare a retry strategy that has succeeded at a specific point. |
| 149 | + let mut retry = BackoffRetry::new(base, max, 2); |
| 150 | + retry.reset_interval = reset_interval; |
| 151 | + let start = Instant::now() - Duration::from_secs(60); |
| 152 | + retry.reset(start); |
| 153 | + |
| 154 | + // Verify that calculating the next delay returns as expected |
| 155 | + let time = start.add(Duration::from_secs(1)); |
| 156 | + let delay = retry.next_delay(time); |
| 157 | + assert_eq!(delay, base); |
| 158 | + |
| 159 | + // Verify resetting the last known good time doesn't change the retry policy since it hasn't |
| 160 | + // exceeded the retry interval. |
| 161 | + let time = time.add(delay); |
| 162 | + retry.reset(time); |
| 163 | + |
| 164 | + let time = time.add(Duration::from_secs(10)); |
| 165 | + let delay = retry.next_delay(time); |
| 166 | + assert_eq!(delay, base * 2); |
| 167 | + |
| 168 | + // And finally check that if we exceed the reset interval, the retry strategy will default |
| 169 | + // back to base. |
| 170 | + let time = time.add(delay); |
| 171 | + retry.reset(time); |
| 172 | + |
| 173 | + let time = time.add(reset_interval); |
| 174 | + let delay = retry.next_delay(time); |
| 175 | + assert_eq!(delay, base); |
| 176 | + } |
| 177 | +} |
0 commit comments