Skip to content

Commit

Permalink
feat: Add std support for no-std without global rand seed (Xuanwo#169)
Browse files Browse the repository at this point in the history
Split Xuanwo#168 into two PRs as
requested, this is the one for no_std support.

---------

Co-authored-by: Xuanwo <[email protected]>
  • Loading branch information
wackazong and Xuanwo committed Dec 17, 2024
1 parent 7339faa commit e42e968
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ targets = [
[features]
default = ["std", "std-blocking-sleep", "tokio-sleep", "gloo-timers-sleep"]
std = ["fastrand/std"]
default = ["std", "std-blocking-sleep", "tokio-sleep", "gloo-timers-sleep"]
std = ["fastrand/std"]
std-blocking-sleep = []
gloo-timers-sleep = ["gloo-timers/futures"]
tokio-sleep = ["tokio/time"]
Expand Down
25 changes: 25 additions & 0 deletions backon/src/backoff/exponential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct ExponentialBuilder {
max_delay: Option<Duration>,
max_times: Option<usize>,
seed: Option<u64>,
seed: Option<u64>,
}

impl Default for ExponentialBuilder {
Expand All @@ -53,11 +54,13 @@ impl Default for ExponentialBuilder {
max_delay: Some(Duration::from_secs(60)),
max_times: Some(3),
seed: None,
seed: None,
}
}
}

impl ExponentialBuilder {
/// Enable jitter for the backoff.
/// Enable jitter for the backoff.
///
/// When jitter is enabled, [`ExponentialBackoff`] will add a random jitter within `(0, min_delay)`
Expand All @@ -73,6 +76,12 @@ impl ExponentialBuilder {
self
}

/// Set the seed value for the jitter random number generator. If no seed is given, a random seed is used in std and default seed is used in no_std.
pub fn with_jitter_seed(mut self, seed: u64) -> Self {
self.seed = Some(seed);
self
}

/// Set the factor for the backoff.
///
/// # Panics
Expand Down Expand Up @@ -145,6 +154,17 @@ impl BackoffBuilder for ExponentialBuilder {

rng
},
rng: if let Some(seed) = self.seed {
fastrand::Rng::with_seed(seed)
} else {
#[cfg(feature = "std")]
let rng = fastrand::Rng::new();

#[cfg(not(feature = "std"))]
let rng = fastrand::Rng::with_seed(super::RANDOM_SEED);

rng
},
factor: self.factor,
min_delay: self.min_delay,
max_delay: self.max_delay,
Expand Down Expand Up @@ -172,6 +192,7 @@ impl BackoffBuilder for &ExponentialBuilder {
pub struct ExponentialBackoff {
jitter: bool,
rng: fastrand::Rng,
rng: fastrand::Rng,
factor: f32,
min_delay: Duration,
max_delay: Option<Duration>,
Expand Down Expand Up @@ -215,6 +236,7 @@ impl Iterator for ExponentialBackoff {
// If jitter is enabled, add random jitter based on min delay.
if self.jitter {
tmp_cur = tmp_cur.saturating_add(self.min_delay.mul_f32(self.rng.f32()));
tmp_cur = tmp_cur.saturating_add(self.min_delay.mul_f32(self.rng.f32()));
}
Some(tmp_cur)
}
Expand Down Expand Up @@ -333,6 +355,7 @@ mod tests {
fn test_exponential_max_delay_without_default_1() {
let mut exp = ExponentialBuilder {
jitter: false,
seed: Some(0x2fdb0020ffc7722b),
seed: 0x2fdb0020ffc7722b,
factor: 10_000_000_000_f32,
min_delay: Duration::from_secs(1),
Expand All @@ -351,6 +374,7 @@ mod tests {
fn test_exponential_max_delay_without_default_2() {
let mut exp = ExponentialBuilder {
jitter: true,
seed: Some(0x2fdb0020ffc7722b),
seed: 0x2fdb0020ffc7722b,
factor: 10_000_000_000_f32,
min_delay: Duration::from_secs(10_000_000_000),
Expand All @@ -369,6 +393,7 @@ mod tests {
fn test_exponential_max_delay_without_default_3() {
let mut exp = ExponentialBuilder {
jitter: false,
seed: Some(0x2fdb0020ffc7722b),
seed: 0x2fdb0020ffc7722b,
factor: 10_000_000_000_f32,
min_delay: Duration::from_secs(10_000_000_000),
Expand Down
1 change: 1 addition & 0 deletions backon/src/backoff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub use exponential::ExponentialBackoff;
pub use exponential::ExponentialBuilder;

// Random seed value for no_std (the value is "backon" in hex)
#[cfg(not(feature = "std"))]
const RANDOM_SEED: u64 = 0x6261636b6f6e;

0 comments on commit e42e968

Please sign in to comment.