1- //! Module implementing an Open Metrics histogram .
1+ //! Module implementing an Open Metrics summary .
22//!
33//! See [`Summary`] for details.
44
55use crate :: encoding:: { EncodeMetric , MetricEncoder } ;
66
77use 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 ;
1110use std:: time:: { Duration , Instant } ;
1211
1312use 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
2625impl 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