-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Ensure connection pool metrics stay consistent #99
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
use std::{ | ||
marker::PhantomData, | ||
sync::{ | ||
atomic::{AtomicU64, Ordering}, | ||
Arc, | ||
}, | ||
time::{Duration, Instant}, | ||
}; | ||
|
||
use metrics::counter; | ||
use tokio::sync::OwnedSemaphorePermit; | ||
|
||
use crate::metrics_utils::{ | ||
GaugeGuard, ACTIVE_CONNECTIONS, CLOSED_TOTAL, IDLE_CONNECTIONS, OPENED_TOTAL, OPEN_CONNECTIONS, | ||
}; | ||
|
||
pub(crate) struct ActiveConn<C> { | ||
inner: C, | ||
state: ConnState, | ||
_permit: OwnedSemaphorePermit, | ||
_active_connections_gauge: GaugeGuard, | ||
} | ||
|
||
impl<C> ActiveConn<C> { | ||
pub(crate) fn new(inner: C, permit: OwnedSemaphorePermit, state: ConnState) -> ActiveConn<C> { | ||
Self { | ||
inner, | ||
state, | ||
_permit: permit, | ||
_active_connections_gauge: GaugeGuard::increment(ACTIVE_CONNECTIONS), | ||
} | ||
} | ||
|
||
pub(crate) fn into_idle(self) -> IdleConn<C> { | ||
IdleConn { | ||
inner: self.inner, | ||
state: self.state, | ||
_idle_connections_gauge: GaugeGuard::increment(IDLE_CONNECTIONS), | ||
} | ||
} | ||
|
||
pub(crate) fn is_brand_new(&self) -> bool { | ||
self.state.brand_new | ||
} | ||
|
||
pub(crate) fn set_brand_new(&mut self, brand_new: bool) { | ||
self.state.brand_new = brand_new | ||
SevInf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pub(crate) fn into_raw(self) -> C { | ||
self.inner | ||
} | ||
|
||
pub(crate) fn as_raw_ref(&self) -> &C { | ||
&self.inner | ||
} | ||
|
||
pub(crate) fn as_raw_mut(&mut self) -> &mut C { | ||
&mut self.inner | ||
} | ||
} | ||
|
||
pub(crate) struct IdleConn<C> { | ||
inner: C, | ||
state: ConnState, | ||
_idle_connections_gauge: GaugeGuard, | ||
} | ||
|
||
impl<C> IdleConn<C> { | ||
pub(crate) fn is_brand_new(&self) -> bool { | ||
self.state.brand_new | ||
} | ||
|
||
pub(crate) fn into_active(self, permit: OwnedSemaphorePermit) -> ActiveConn<C> { | ||
ActiveConn::new(self.inner, permit, self.state) | ||
} | ||
|
||
pub(crate) fn created_at(&self) -> Instant { | ||
self.state.created_at | ||
} | ||
|
||
pub(crate) fn expired(&self, timeout: Option<Duration>) -> bool { | ||
timeout | ||
.and_then(|check_interval| { | ||
Instant::now() | ||
.checked_duration_since(self.state.created_at) | ||
.map(|dur_since| dur_since >= check_interval) | ||
}) | ||
.unwrap_or(false) | ||
} | ||
|
||
pub(crate) fn idle_expired(&self, timeout: Option<Duration>) -> bool { | ||
timeout | ||
.and_then(|check_interval| { | ||
Instant::now() | ||
.checked_duration_since(self.state.last_used_at) | ||
.map(|dur_since| dur_since >= check_interval) | ||
}) | ||
.unwrap_or(false) | ||
} | ||
|
||
pub(crate) fn needs_health_check(&self, timeout: Option<Duration>) -> bool { | ||
timeout | ||
.and_then(|check_interval| { | ||
Instant::now() | ||
.checked_duration_since(self.state.last_checked_at) | ||
.map(|dur_since| dur_since >= check_interval) | ||
}) | ||
.unwrap_or(true) | ||
} | ||
|
||
pub(crate) fn mark_checked(&mut self) { | ||
self.state.last_checked_at = Instant::now() | ||
} | ||
|
||
pub(crate) fn split_raw(self) -> (C, ConnSplit<C>) { | ||
( | ||
self.inner, | ||
ConnSplit::new(self.state, self._idle_connections_gauge), | ||
) | ||
} | ||
} | ||
|
||
pub(crate) struct ConnState { | ||
pub(crate) created_at: Instant, | ||
pub(crate) last_used_at: Instant, | ||
pub(crate) last_checked_at: Instant, | ||
pub(crate) brand_new: bool, | ||
total_connections_open: Arc<AtomicU64>, | ||
total_connections_closed: Arc<AtomicU64>, | ||
_open_connections_gauge: GaugeGuard, | ||
} | ||
|
||
impl ConnState { | ||
pub(crate) fn new( | ||
total_connections_open: Arc<AtomicU64>, | ||
total_connections_closed: Arc<AtomicU64>, | ||
) -> Self { | ||
counter!(OPENED_TOTAL).increment(1); | ||
Self { | ||
created_at: Instant::now(), | ||
last_used_at: Instant::now(), | ||
last_checked_at: Instant::now(), | ||
brand_new: true, | ||
total_connections_open, | ||
total_connections_closed, | ||
_open_connections_gauge: GaugeGuard::increment(OPEN_CONNECTIONS), | ||
} | ||
} | ||
} | ||
|
||
impl Drop for ConnState { | ||
fn drop(&mut self) { | ||
self.total_connections_open.fetch_sub(1, Ordering::Relaxed); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've just moved existing code around, but I agree with you here, acquire-release makes more sense in this case |
||
self.total_connections_closed | ||
.fetch_add(1, Ordering::Relaxed); | ||
counter!(CLOSED_TOTAL).increment(1); | ||
} | ||
} | ||
|
||
pub(crate) struct ConnSplit<C> { | ||
state: ConnState, | ||
gauge: GaugeGuard, | ||
_phantom: PhantomData<C>, | ||
} | ||
|
||
impl<C> ConnSplit<C> { | ||
fn new(state: ConnState, gauge: GaugeGuard) -> Self { | ||
Self { | ||
state, | ||
gauge, | ||
_phantom: PhantomData, | ||
} | ||
} | ||
|
||
pub(crate) fn restore(self, raw: C) -> IdleConn<C> { | ||
IdleConn { | ||
inner: raw, | ||
state: self.state, | ||
_idle_connections_gauge: self.gauge, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you keep the permit instead of forgetting it? I can't see you using it anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I forget it, i will then need to add it back manually when connection drops. If I keep on struct, it will automatically gets returned when the struct drops, we don't need to think about doing manual management and we can't get it wrong in case struct drops earlier then we thought.