|
10 | 10 | use core::time::Duration;
|
11 | 11 |
|
12 | 12 | pub(crate) struct BatchDelay {
|
13 |
| - next_batch_delay_millis: u16, |
| 13 | + next_batch_delay: Duration, |
14 | 14 | }
|
15 | 15 |
|
16 | 16 | impl BatchDelay {
|
17 | 17 | pub(crate) fn new() -> Self {
|
18 |
| - let next_batch_delay_millis = rand_batch_delay_millis(); |
19 |
| - Self { next_batch_delay_millis } |
| 18 | + let next_batch_delay = rand_batch_delay(); |
| 19 | + Self { next_batch_delay } |
20 | 20 | }
|
21 | 21 |
|
22 | 22 | pub(crate) fn get(&self) -> Duration {
|
23 |
| - Duration::from_millis(self.next_batch_delay_millis as u64) |
| 23 | + self.next_batch_delay |
24 | 24 | }
|
25 | 25 |
|
26 | 26 | pub(crate) fn next(&mut self) -> Duration {
|
27 |
| - let next = rand_batch_delay_millis(); |
28 |
| - self.next_batch_delay_millis = next; |
29 |
| - Duration::from_millis(next as u64) |
| 27 | + let next = rand_batch_delay(); |
| 28 | + self.next_batch_delay = next; |
| 29 | + next |
30 | 30 | }
|
31 | 31 | }
|
32 | 32 |
|
33 |
| -fn rand_batch_delay_millis() -> u16 { |
| 33 | +fn rand_batch_delay() -> Duration { |
| 34 | + let delay_millis = rand_delay_millis(); |
| 35 | + let delay_micros = rand_delay_micros(); |
| 36 | + Duration::from_millis(delay_millis) + Duration::from_micros(delay_micros) |
| 37 | +} |
| 38 | + |
| 39 | +fn rand_delay_millis() -> u64 { |
| 40 | + // We draw milliseconds from the log-normal FWD_DELAYS_MILLIS below. |
34 | 41 | const USIZE_LEN: usize = core::mem::size_of::<usize>();
|
35 | 42 | let mut random_bytes = [0u8; USIZE_LEN];
|
36 | 43 | possiblyrandom::getpossiblyrandom(&mut random_bytes);
|
37 | 44 |
|
38 | 45 | let index = usize::from_be_bytes(random_bytes) % FWD_DELAYS_MILLIS.len();
|
39 |
| - FWD_DELAYS_MILLIS[index] |
| 46 | + FWD_DELAYS_MILLIS[index] as u64 |
| 47 | +} |
| 48 | + |
| 49 | +fn rand_delay_micros() -> u64 { |
| 50 | + // We draw microseconds uniformly in [0; 1000). |
| 51 | + let mut random_bytes = [0u8; 2]; |
| 52 | + possiblyrandom::getpossiblyrandom(&mut random_bytes); |
| 53 | + let micros = u16::from_be_bytes(random_bytes) % 1000; |
| 54 | + micros as u64 |
40 | 55 | }
|
41 | 56 |
|
42 | 57 | // An array of potential forwarding delays (log-normal distribution, 10000 samples, mean = 50, sd = 0.5), generated via the following R-script:
|
|
0 commit comments