Skip to content

Commit

Permalink
fix: remove std:time::Instant to restore WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
c-git committed Jan 17, 2025
1 parent a7c5f27 commit d992bc8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ rand = "0.8.5"
reqwest = { version = "0.12.12", default-features = false }
thiserror = "2.0.9"
tracing = "0.1.41"
web-time = "1.1.0"

# For native compilation
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
36 changes: 19 additions & 17 deletions src/data_state_retry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use tracing::warn;
use crate::{data_state::CanMakeProgress, Awaiting, DataState, ErrorBounds};
use std::fmt::Debug;
use std::ops::Range;
use std::time::{Duration, Instant};

/// Automatically retries with a delay on failure until attempts are exhausted
#[derive(Debug)]
Expand All @@ -17,7 +16,7 @@ pub struct DataStateRetry<T, E: ErrorBounds = anyhow::Error> {

attempts_left: u8,
inner: DataState<T, E>, // Not public to ensure resets happen as they should
next_allowed_attempt: Instant,
next_allowed_attempt: u128,
}

impl<T, E: ErrorBounds> DataStateRetry<T, E> {
Expand All @@ -35,9 +34,8 @@ impl<T, E: ErrorBounds> DataStateRetry<T, E> {
self.attempts_left
}

/// The instant that needs to be waited for before another attempt is
/// allowed
pub fn next_allowed_attempt(&self) -> Instant {
/// The number of millis after the epoch that an attempt is allowed
pub fn next_allowed_attempt(&self) -> u128 {
self.next_allowed_attempt
}

Expand Down Expand Up @@ -112,7 +110,7 @@ impl<T, E: ErrorBounds> DataStateRetry<T, E> {
format!(
"{} attempt(s) left. {} seconds before retry. {e}",
self.attempts_left,
wait_left.as_secs()
wait_left / 1000
),
);
if ui.button("Stop Trying").clicked() {
Expand Down Expand Up @@ -140,12 +138,9 @@ impl<T, E: ErrorBounds> DataStateRetry<T, E> {
DataState::None => {
// Going to make an attempt, set when the next attempt is allowed
use rand::Rng as _;
let wait_time_in_millis = rand::thread_rng()
.gen_range(self.retry_delay_millis.clone())
.into();
self.next_allowed_attempt = Instant::now()
.checked_add(Duration::from_millis(wait_time_in_millis))
.expect("failed to get random delay, value was out of range");
let wait_time_in_millis =
rand::thread_rng().gen_range(self.retry_delay_millis.clone());
self.next_allowed_attempt = millis_since_epoch() + wait_time_in_millis as u128;

self.inner.start_request(fetch_fn)
}
Expand All @@ -162,7 +157,7 @@ impl<T, E: ErrorBounds> DataStateRetry<T, E> {
CanMakeProgress::UnableToMakeProgress
} else {
let wait_left = wait_before_next_attempt(self.next_allowed_attempt);
if wait_left.is_zero() {
if wait_left == 0 {
warn!(?err_msg, ?self.attempts_left, "retrying request");
self.attempts_left -= 1;
self.inner = DataState::None;
Expand All @@ -176,7 +171,7 @@ impl<T, E: ErrorBounds> DataStateRetry<T, E> {
/// Resets the attempts taken
pub fn reset_attempts(&mut self) {
self.attempts_left = self.max_attempts;
self.next_allowed_attempt = Instant::now();
self.next_allowed_attempt = millis_since_epoch();
}

/// Clear stored data
Expand Down Expand Up @@ -214,7 +209,7 @@ impl<T, E: ErrorBounds> Default for DataStateRetry<T, E> {
max_attempts: 3,
retry_delay_millis: 1000..5000,
attempts_left: 3,
next_allowed_attempt: Instant::now(),
next_allowed_attempt: millis_since_epoch(),
}
}
}
Expand All @@ -232,8 +227,15 @@ impl<T, E: ErrorBounds> AsMut<DataStateRetry<T, E>> for DataStateRetry<T, E> {
}

/// The duration before the next attempt will be made
fn wait_before_next_attempt(next_allowed_attempt: Instant) -> Duration {
next_allowed_attempt.saturating_duration_since(Instant::now())
fn wait_before_next_attempt(next_allowed_attempt: u128) -> u128 {
next_allowed_attempt.saturating_sub(millis_since_epoch())
}

fn millis_since_epoch() -> u128 {
web_time::SystemTime::UNIX_EPOCH
.elapsed()
.expect("expected date on system to be after the epoch")
.as_millis()
}

// TODO 4: Use mocking to add tests ensuring retires are executed

0 comments on commit d992bc8

Please sign in to comment.