Fix problem with counting the PoolGuard

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/16/head
trivernis 3 years ago
parent 66c81605fc
commit 73e5a672b8
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -88,7 +88,6 @@ pub struct PooledContext {
contexts: Vec<PoolGuard<Context>>, contexts: Vec<PoolGuard<Context>>,
} }
#[derive(Clone)]
pub struct PoolGuard<T> pub struct PoolGuard<T>
where where
T: Clone, T: Clone,
@ -117,6 +116,20 @@ where
} }
} }
impl<T> Clone for PoolGuard<T>
where
T: Clone,
{
fn clone(&self) -> Self {
self.acquire();
Self {
inner: self.inner.clone(),
count: Arc::clone(&self.count),
}
}
}
impl<T> Drop for PoolGuard<T> impl<T> Drop for PoolGuard<T>
where where
T: Clone, T: Clone,
@ -137,19 +150,18 @@ where
} }
} }
/// Acquires the context by adding 1 to the count and /// Acquires the context by adding 1 to the count
/// returning the cloned variant
#[tracing::instrument(level = "trace", skip_all)] #[tracing::instrument(level = "trace", skip_all)]
pub(crate) fn acquire(&self) -> Self { pub(crate) fn acquire(&self) {
self.count.fetch_add(1, Ordering::Relaxed); let count = self.count.fetch_add(1, Ordering::Relaxed);
tracing::trace!(count);
self.clone()
} }
/// Releases the connection by subtracting from the stored count /// Releases the connection by subtracting from the stored count
#[tracing::instrument(level = "trace", skip_all)] #[tracing::instrument(level = "trace", skip_all)]
pub(crate) fn release(&self) { pub(crate) fn release(&self) {
self.count.fetch_sub(1, Ordering::Relaxed); let count = self.count.fetch_sub(1, Ordering::Relaxed);
tracing::trace!(count);
} }
pub(crate) fn count(&self) -> usize { pub(crate) fn count(&self) -> usize {
@ -173,6 +185,6 @@ impl PooledContext {
.iter() .iter()
.min_by_key(|c| c.count()) .min_by_key(|c| c.count())
.unwrap() .unwrap()
.acquire() .clone()
} }
} }

Loading…
Cancel
Save