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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
use embedded_time::duration::Milliseconds;
use embedded_time::{Clock, Instant};
#[derive(Debug, Clone, Copy)]
pub struct RetryTimer<C: Clock<T = u64>> {
start: Instant<C>,
strategy: Strategy,
attempts: Attempts,
max_attempts: Attempts,
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Attempts(pub u16);
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub enum YouShould {
Cry,
Retry,
}
impl<C: Clock<T = u64>> RetryTimer<C> {
pub fn new(start: Instant<C>, strategy: Strategy, max_attempts: Attempts) -> Self {
Self { start,
strategy,
max_attempts,
attempts: Attempts(1) }
}
pub fn what_should_i_do(&mut self, now: Instant<C>) -> nb::Result<YouShould, core::convert::Infallible> {
if self.attempts >= self.max_attempts {
Ok(YouShould::Cry)
} else {
let ready = self.strategy
.is_ready((now - self.start).try_into().unwrap(), self.attempts.0);
if ready {
self.attempts.0 += 1;
Ok(YouShould::Retry)
} else {
Err(nb::Error::WouldBlock)
}
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum Strategy {
Exponential(Milliseconds<u64>),
Delay(Milliseconds<u64>),
}
impl Strategy {
pub fn is_ready(&self, time_passed: Milliseconds<u64>, attempts: u16) -> bool {
if attempts == 0 {
return true;
}
match self {
| Self::Delay(dur) => time_passed.0 >= (dur.0 * attempts as u64),
| Self::Exponential(dur) => time_passed.0 >= Self::total_delay_exp(*dur, attempts),
}
}
fn total_delay_exp(init: Milliseconds<u64>, attempts: u16) -> u64 {
match attempts {
| 1 => init.0,
| n => init.0 + (1..n).map(|n| init.0 * 2u64.pow(n as u32)).sum::<u64>(),
}
}
}
#[cfg(test)]
mod test {
use embedded_time::rate::Fraction;
use super::*;
pub struct FakeClock(pub *const u64);
impl FakeClock {
pub fn new(time_ptr: *const u64) -> Self {
Self(time_ptr)
}
}
impl Clock for FakeClock {
type T = u64;
const SCALING_FACTOR: Fraction = Fraction::new(1, 1000);
fn try_now(&self) -> Result<Instant<Self>, embedded_time::clock::Error> {
unsafe { Ok(Instant::new(*self.0)) }
}
}
#[test]
fn retrier() {
#![allow(unused_assignments)]
let mut time_millis = 0u64;
let clock = FakeClock::new(&time_millis as *const _);
let now = || clock.try_now().unwrap();
let mut retry = RetryTimer::new(now(), Strategy::Delay(Milliseconds(1000)), Attempts(5));
time_millis = 999;
assert_eq!(retry.what_should_i_do(now()).unwrap_err(), nb::Error::WouldBlock);
time_millis = 1000;
assert_eq!(retry.what_should_i_do(now()).unwrap(), YouShould::Retry);
time_millis = 1999;
assert_eq!(retry.what_should_i_do(now()).unwrap_err(), nb::Error::WouldBlock);
time_millis = 2000;
assert_eq!(retry.what_should_i_do(now()).unwrap(), YouShould::Retry);
time_millis = 10_000;
assert_eq!(retry.what_should_i_do(now()).unwrap(), YouShould::Retry);
assert_eq!(retry.what_should_i_do(now()).unwrap(), YouShould::Retry);
assert_eq!(retry.what_should_i_do(now()).unwrap(), YouShould::Cry);
}
#[test]
fn delay_waits() {
let strat = Strategy::Delay(Milliseconds(100));
assert!(strat.is_ready(Milliseconds(0), 0));
assert!(!strat.is_ready(Milliseconds(99), 1));
assert!(strat.is_ready(Milliseconds(100), 1));
assert!(!strat.is_ready(Milliseconds(199), 2));
assert!(strat.is_ready(Milliseconds(200), 2));
assert!(!strat.is_ready(Milliseconds(299), 3));
assert!(strat.is_ready(Milliseconds(300), 3));
}
#[test]
fn exp_calculation() {
let init = Milliseconds(100);
assert_eq!(Strategy::total_delay_exp(init, 1), 100);
assert_eq!(Strategy::total_delay_exp(init, 2), 300);
assert_eq!(Strategy::total_delay_exp(init, 3), 700);
}
#[test]
fn exp_waits() {
let strat = Strategy::Exponential(Milliseconds(100));
assert!(strat.is_ready(Milliseconds(0), 0));
assert!(!strat.is_ready(Milliseconds(99), 1));
assert!(strat.is_ready(Milliseconds(100), 1));
assert!(!strat.is_ready(Milliseconds(299), 2));
assert!(strat.is_ready(Milliseconds(300), 2));
assert!(!strat.is_ready(Milliseconds(699), 3));
assert!(strat.is_ready(Milliseconds(700), 3));
}
}