#[cfg(not(loom))]
use std::sync::atomic::{AtomicU64, Ordering};
#[cfg(loom)]
use loom::sync::atomic::{AtomicU64, Ordering};
#[cfg(not(loom))]
static ACTIVE_CONNECTIONS: AtomicU64 = AtomicU64::new(0);
#[cfg(loom)]
loom::lazy_static! {
static ref ACTIVE_CONNECTIONS: AtomicU64 = AtomicU64::new(0);
}
pub(crate) struct ActiveConnection;
impl ActiveConnection {
pub(crate) fn new() -> Self {
ACTIVE_CONNECTIONS.fetch_add(1, Ordering::Relaxed);
crate::metrics::inc_connections();
Self
}
}
impl Drop for ActiveConnection {
fn drop(&mut self) {
ACTIVE_CONNECTIONS.fetch_sub(1, Ordering::Relaxed);
crate::metrics::dec_connections();
}
}
#[cfg(loom)]
pub struct LoomConnectionGuard(ActiveConnection);
#[cfg(loom)]
impl LoomConnectionGuard {
#[must_use]
pub fn new() -> Self { Self(ActiveConnection::new()) }
}
#[must_use]
pub fn active_connection_count() -> u64 { ACTIVE_CONNECTIONS.load(Ordering::Relaxed) }
pub(super) fn current_count() -> u64 { ACTIVE_CONNECTIONS.load(Ordering::Relaxed) }