Skip to content

Commit 29f3d4b

Browse files
committed
Address review comments
Signed-off-by: Palash Nigam <[email protected]>
1 parent 9462cd6 commit 29f3d4b

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Diff for: src/metrics/summary.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
//! Module implementing an Open Metrics histogram.
1+
//! Module implementing an Open Metrics summary.
22
//!
33
//! See [`Summary`] for details.
44
55
use crate::encoding::{EncodeMetric, MetricEncoder};
66

77
use super::{MetricType, TypedMetric};
8-
//use owning_ref::OwningRef;
9-
//use std::iter::{self, once};
10-
use std::sync::{Arc, Mutex};
8+
use parking_lot::RwLock;
9+
use std::sync::Arc;
1110
use std::time::{Duration, Instant};
1211

1312
use quantiles::ckms::CKMS;
@@ -20,7 +19,7 @@ pub struct Summary {
2019
max_age_buckets: u64,
2120
max_age_seconds: u64,
2221
stream_duration: Duration,
23-
inner: Arc<Mutex<InnerSummary>>,
22+
inner: Arc<RwLock<InnerSummary>>,
2423
}
2524

2625
impl Clone for Summary {
@@ -69,7 +68,7 @@ impl Summary {
6968
stream_duration,
7069
target_quantile,
7170
target_error,
72-
inner: Arc::new(Mutex::new(InnerSummary {
71+
inner: Arc::new(RwLock::new(InnerSummary {
7372
sum: Default::default(),
7473
count: Default::default(),
7574
quantile_streams: streams,
@@ -83,7 +82,7 @@ impl Summary {
8382
pub fn observe(&self, v: f64) {
8483
self.rotate_buckets();
8584

86-
let mut inner = self.inner.lock().unwrap();
85+
let mut inner = self.inner.write();
8786
inner.sum += v;
8887
inner.count += 1;
8988

@@ -97,7 +96,7 @@ impl Summary {
9796
pub fn get(&self) -> (f64, u64, Vec<(f64, f64)>) {
9897
self.rotate_buckets();
9998

100-
let inner = self.inner.lock().unwrap();
99+
let inner = self.inner.read();
101100
let sum = inner.sum;
102101
let count = inner.count;
103102
let mut quantile_values: Vec<(f64, f64)> = Vec::new();
@@ -112,7 +111,7 @@ impl Summary {
112111
}
113112

114113
fn rotate_buckets(&self) {
115-
let mut inner = self.inner.lock().unwrap();
114+
let mut inner = self.inner.write();
116115
if inner.last_rotated_timestamp.elapsed() >= self.stream_duration {
117116
inner.last_rotated_timestamp = Instant::now();
118117
if inner.head_stream_idx == self.max_age_buckets {

0 commit comments

Comments
 (0)