1
- use log:: { debug, error, info} ;
2
- use once_cell:: sync:: OnceCell ;
1
+ use log:: { debug, info} ;
2
+ use once_cell:: sync:: Lazy ;
3
+ use parking_lot:: Mutex ;
3
4
use statsd:: Client ;
4
- /// Events collector and publisher.
5
5
use tokio:: sync:: mpsc:: { Receiver , Sender } ;
6
6
7
7
use std:: collections:: HashMap ;
8
- use std:: sync:: { Arc , Mutex } ;
9
8
10
9
use crate :: config:: get_config;
11
10
12
- static LATEST_STATS : OnceCell < Arc < Mutex < HashMap < String , i64 > > > > = OnceCell :: new ( ) ;
11
+ // Stats used in SHOW STATS
12
+ static LATEST_STATS : Lazy < Mutex < HashMap < String , i64 > > > = Lazy :: new ( || Mutex :: new ( HashMap :: new ( ) ) ) ;
13
13
static STAT_PERIOD : u64 = 15000 ; //15 seconds
14
14
15
15
#[ derive( Debug , Clone , Copy ) ]
@@ -187,16 +187,6 @@ impl Reporter {
187
187
188
188
let _ = self . tx . try_send ( event) ;
189
189
}
190
-
191
- // pub fn flush_to_statsd(&self) {
192
- // let event = Event {
193
- // name: EventName::FlushStatsToStatsD,
194
- // value: 0,
195
- // process_id: None,
196
- // };
197
-
198
- // let _ = self.tx.try_send(event);
199
- // }
200
190
}
201
191
202
192
pub struct Collector {
@@ -217,13 +207,6 @@ impl Collector {
217
207
pub async fn collect ( & mut self ) {
218
208
info ! ( "Events reporter started" ) ;
219
209
220
- match LATEST_STATS . set ( Arc :: new ( Mutex :: new ( HashMap :: new ( ) ) ) ) {
221
- Ok ( _) => ( ) ,
222
- Err ( _) => {
223
- error ! ( "Latest stats will not be available" ) ;
224
- }
225
- } ;
226
-
227
210
let mut stats = HashMap :: from ( [
228
211
( "total_query_count" , 0 ) ,
229
212
( "total_xact_count" , 0 ) ,
@@ -400,16 +383,10 @@ impl Collector {
400
383
debug ! ( "{:?}" , stats) ;
401
384
402
385
// Update latest stats used in SHOW STATS
403
- match LATEST_STATS . get ( ) {
404
- Some ( arc) => {
405
- let mut guard = arc. lock ( ) . unwrap ( ) ;
406
- for ( key, value) in & stats {
407
- guard. insert ( key. to_string ( ) , value. clone ( ) ) ;
408
- }
409
- }
410
-
411
- None => ( ) ,
412
- } ;
386
+ let mut guard = LATEST_STATS . lock ( ) ;
387
+ for ( key, value) in & stats {
388
+ guard. insert ( key. to_string ( ) , value. clone ( ) ) ;
389
+ }
413
390
414
391
let mut pipeline = self . client . pipeline ( ) ;
415
392
@@ -440,13 +417,6 @@ impl Collector {
440
417
}
441
418
}
442
419
443
- pub fn get_stats ( ) -> Option < HashMap < String , i64 > > {
444
- match LATEST_STATS . get ( ) {
445
- Some ( arc) => {
446
- let guard = arc. lock ( ) . unwrap ( ) ;
447
- Some ( guard. clone ( ) )
448
- }
449
-
450
- None => None ,
451
- }
420
+ pub fn get_stats ( ) -> HashMap < String , i64 > {
421
+ LATEST_STATS . lock ( ) . clone ( )
452
422
}
0 commit comments