Skip to content

Commit

Permalink
Don't use Instant::now on WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
shadaj committed Apr 22, 2024
1 parent 5a71bca commit ff8a387
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions postgres/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions src/managed/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#[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<Instant>,
/// The number of times the objects was recycled
pub recycle_count: usize,
}

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()
Expand All @@ -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,
}
Expand Down
10 changes: 8 additions & 2 deletions src/managed/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -409,7 +412,10 @@ impl<M: Manager, W: From<Object<M>>> Pool<M, W> {
}

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()))
}
Expand Down

0 comments on commit ff8a387

Please sign in to comment.