From d992bc85d4269e6227b0a737f56c968a077d0211 Mon Sep 17 00:00:00 2001 From: One <43485962+c-git@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:11:38 -0500 Subject: [PATCH 1/4] fix: remove std:time::Instant to restore WASM --- Cargo.lock | 1 + Cargo.toml | 1 + src/data_state_retry.rs | 36 +++++++++++++++++++----------------- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 54d7e03..347affa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1399,6 +1399,7 @@ dependencies = [ "wasm-bindgen-futures", "wasm-bindgen-test", "web-sys", + "web-time", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 3268449..6d23f3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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] diff --git a/src/data_state_retry.rs b/src/data_state_retry.rs index 34ba9d6..a0294a5 100644 --- a/src/data_state_retry.rs +++ b/src/data_state_retry.rs @@ -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)] @@ -17,7 +16,7 @@ pub struct DataStateRetry { attempts_left: u8, inner: DataState, // Not public to ensure resets happen as they should - next_allowed_attempt: Instant, + next_allowed_attempt: u128, } impl DataStateRetry { @@ -35,9 +34,8 @@ impl DataStateRetry { 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 } @@ -112,7 +110,7 @@ impl DataStateRetry { format!( "{} attempt(s) left. {} seconds before retry. {e}", self.attempts_left, - wait_left.as_secs() + wait_left / 1000 ), ); if ui.button("Stop Trying").clicked() { @@ -140,12 +138,9 @@ impl DataStateRetry { 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) } @@ -162,7 +157,7 @@ impl DataStateRetry { 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; @@ -176,7 +171,7 @@ impl DataStateRetry { /// 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 @@ -214,7 +209,7 @@ impl Default for DataStateRetry { max_attempts: 3, retry_delay_millis: 1000..5000, attempts_left: 3, - next_allowed_attempt: Instant::now(), + next_allowed_attempt: millis_since_epoch(), } } } @@ -232,8 +227,15 @@ impl AsMut> for DataStateRetry { } /// 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 From 421fe4c8f5d0bbaf78c93fe6234c280a6e79000a Mon Sep 17 00:00:00 2001 From: One <43485962+c-git@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:13:10 -0500 Subject: [PATCH 2/4] chore: save todo --- src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 587ea68..13c4195 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,3 +89,6 @@ pub use yield_::yield_now; // Exported to ensure version used matches pub use futures::channel::oneshot; pub use reqwest; + +// TODO 4: Add browser test to ensure we don't break WASM by accident. Even if +// it can compile it might not be browser safe From 3e101a06e97c88820db108c99145baaa67d3b0d1 Mon Sep 17 00:00:00 2001 From: One <43485962+c-git@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:22:50 -0500 Subject: [PATCH 3/4] fix: change order of code to prevent panic If attempts is set to 0 then the call to poll will return no progress possible and cause a panic --- src/data_state_retry.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/data_state_retry.rs b/src/data_state_retry.rs index a0294a5..4631577 100644 --- a/src/data_state_retry.rs +++ b/src/data_state_retry.rs @@ -113,14 +113,14 @@ impl DataStateRetry { wait_left / 1000 ), ); - if ui.button("Stop Trying").clicked() { - self.attempts_left = 0; - } let can_make_progress = self.start_or_poll(fetch_fn); debug_assert!( can_make_progress.is_able_to_make_progress(), "This should be able to make progress" ); + if ui.button("Stop Trying").clicked() { + self.attempts_left = 0; + } } CanMakeProgress::AbleToMakeProgress } From 781ef12b0f60fe8c578af04f91435e0f51416f03 Mon Sep 17 00:00:00 2001 From: One <43485962+c-git@users.noreply.github.com> Date: Thu, 16 Jan 2025 23:36:42 -0500 Subject: [PATCH 4/4] chore: version bump 0.6.2 --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 347affa..657a25a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1383,7 +1383,7 @@ dependencies = [ [[package]] name = "reqwest-cross" -version = "0.6.1" +version = "0.6.2" dependencies = [ "anyhow", "document-features", diff --git a/Cargo.toml b/Cargo.toml index 6d23f3c..138790d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "reqwest-cross" -version = "0.6.1" +version = "0.6.2" authors = ["One "] categories = ["web-programming::http-client", "wasm"] documentation = "https://docs.rs/reqwest-cross"