From 776b59fa2bbc4f296a9646c5c572848e70bc74b2 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 --- .github/workflows/ci.yml | 2 -- postgres/Cargo.toml | 3 +++ src/managed/metrics.rs | 7 +++++++ src/managed/mod.rs | 10 ++++++++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f233e19f..cd88ea59 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -134,9 +134,7 @@ jobs: - postgres feature: - --features rt_tokio_1 - - --features rt_async-std_1 - --features serde --features rt_tokio_1 - - --features serde --features rt_async-std_1 runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index e10a2b0d..c651a9bb 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 2d4464d6..3c35ade7 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())) }