Skip to content

Commit 0ae20be

Browse files
committed
fix: make the values recorded in wait_duration and histogram consistent
1 parent 04eb7b2 commit 0ae20be

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,7 @@ impl<M: Manager> Pool<M> {
442442

443443
let mut internals = self.0.internals.lock().await;
444444

445-
internals.wait_duration += wait_guard.elapsed();
446-
drop(wait_guard);
445+
internals.wait_duration += wait_guard.into_elapsed();
447446

448447
let conn = internals.free_conns.pop();
449448
let internal_config = internals.config.clone();

src/metrics_utils.rs

+13-5
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,33 @@ impl Drop for GaugeGuard {
5959
}
6060

6161
pub(crate) struct DurationHistogramGuard {
62-
start: Instant,
62+
start: Option<Instant>,
6363
key: &'static str,
6464
}
6565

6666
impl DurationHistogramGuard {
6767
pub(crate) fn start(key: &'static str) -> Self {
6868
Self {
69-
start: Instant::now(),
69+
start: Some(Instant::now()),
7070
key,
7171
}
7272
}
7373

74-
pub(crate) fn elapsed(&self) -> Duration {
75-
self.start.elapsed()
74+
pub(crate) fn into_elapsed(mut self) -> Duration {
75+
let start = self
76+
.start
77+
.take()
78+
.expect("start time was unset without consuming or dropping the guard");
79+
let elapsed = start.elapsed();
80+
histogram!(self.key).record(elapsed);
81+
elapsed
7682
}
7783
}
7884

7985
impl Drop for DurationHistogramGuard {
8086
fn drop(&mut self) {
81-
histogram!(self.key).record(self.start.elapsed());
87+
if let Some(start) = self.start.take() {
88+
histogram!(self.key).record(start.elapsed());
89+
}
8290
}
8391
}

0 commit comments

Comments
 (0)