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 c5ac2ce commit 65f2871
Show file tree
Hide file tree
Showing 2 changed files with 24 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
22 changes: 22 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

0 comments on commit 65f2871

Please sign in to comment.