Skip to content

Commit 8e67c49

Browse files
committed
Also randomize the batch delay at microsecond resolution
In addition to randomizing the millisecond cound, we randomize at the microsecond resolution by drawing from a uniform distribution over the interval [0; 1000).
1 parent f8e8963 commit 8e67c49

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

lightning-background-processor/src/fwd_batch.rs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,48 @@
1010
use core::time::Duration;
1111

1212
pub(crate) struct BatchDelay {
13-
next_batch_delay_millis: u16,
13+
next_batch_delay: Duration,
1414
}
1515

1616
impl BatchDelay {
1717
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 }
2020
}
2121

2222
pub(crate) fn get(&self) -> Duration {
23-
Duration::from_millis(self.next_batch_delay_millis as u64)
23+
self.next_batch_delay
2424
}
2525

2626
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
3030
}
3131
}
3232

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.
3441
const USIZE_LEN: usize = core::mem::size_of::<usize>();
3542
let mut random_bytes = [0u8; USIZE_LEN];
3643
possiblyrandom::getpossiblyrandom(&mut random_bytes);
3744

3845
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
4055
}
4156

4257
// 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

Comments
 (0)