From d52205e3c4a8849ee698f22837fce46395dbfd15 Mon Sep 17 00:00:00 2001 From: Shadaj Laddad Date: Sat, 20 Apr 2024 00:09:39 -0700 Subject: [PATCH] Don't use Instant::now on WASM --- postgres/Cargo.toml | 3 +++ src/managed/metrics.rs | 7 +++++++ src/managed/mod.rs | 10 ++++++++-- 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index 4991d8c7..c575e7d0 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -31,6 +31,9 @@ tokio = { version = "1.29", features = ["rt"] } tokio-postgres = { version = "0.7.9", default-features = false } tracing = "0.1.37" +[target.'cfg(target_arch = "wasm32")'.dependencies] +getrandom = { version = "0.2", features = ["js"] } + [dev-dependencies] config = { version = "0.14", features = ["json"] } dotenvy = "0.15.0" diff --git a/src/managed/metrics.rs b/src/managed/metrics.rs index b2400621..fc41fd6f 100644 --- a/src/managed/metrics.rs +++ b/src/managed/metrics.rs @@ -1,11 +1,14 @@ +#[cfg(not(target_arch = "wasm32"))] use std::time::{Duration, Instant}; /// Statistics regarding an object returned by the pool #[derive(Clone, Copy, Debug)] #[must_use] pub struct Metrics { + #[cfg(not(target_arch = "wasm32"))] /// The instant when this object was created pub created: Instant, + #[cfg(not(target_arch = "wasm32"))] /// The instant when this object was last used pub recycled: Option, /// The number of times the objects was recycled @@ -13,10 +16,12 @@ pub struct Metrics { } impl Metrics { + #[cfg(not(target_arch = "wasm32"))] /// Access the age of this object pub fn age(&self) -> Duration { self.created.elapsed() } + #[cfg(not(target_arch = "wasm32"))] /// Get the time elapsed when this object was last used pub fn last_used(&self) -> Duration { self.recycled.unwrap_or(self.created).elapsed() @@ -26,7 +31,9 @@ impl Metrics { impl Default for Metrics { fn default() -> Self { Self { + #[cfg(not(target_arch = "wasm32"))] created: Instant::now(), + #[cfg(not(target_arch = "wasm32"))] recycled: None, recycle_count: 0, } diff --git a/src/managed/mod.rs b/src/managed/mod.rs index 6fed8150..bee5dc2b 100644 --- a/src/managed/mod.rs +++ b/src/managed/mod.rs @@ -66,9 +66,12 @@ use std::{ atomic::{AtomicUsize, Ordering}, Arc, Mutex, Weak, }, - time::{Duration, Instant}, + time::Duration, }; +#[cfg(not(target_arch = "wasm32"))] +use std::time::Instant; + use deadpool_runtime::Runtime; use tokio::sync::{Semaphore, TryAcquireError}; @@ -409,7 +412,10 @@ impl>> Pool { } inner.metrics.recycle_count += 1; - inner.metrics.recycled = Some(Instant::now()); + #[cfg(not(target_arch = "wasm32"))] + { + inner.metrics.recycled = Some(Instant::now()); + } Ok(Some(unready_obj.ready())) }