@@ -8,12 +8,17 @@ use std::future::Future;
88use tokio:: sync:: mpsc;
99use tracing:: debug;
1010
11+ pub const ACTIVITY_EVENT_QUEUE_CAPACITY : usize = 1024 ;
12+ const ACTIVITY_FLUSH_QUEUE_CAPACITY : usize = 1 ;
13+
1114#[ derive( Debug , Clone ) ]
1215pub struct ActivityEvent {
1316 pub denied : bool ,
1417 pub deny_group : & ' static str ,
1518}
1619
20+ pub type ActivitySender = mpsc:: Sender < ActivityEvent > ;
21+
1722#[ derive( Debug , Clone , PartialEq , Eq ) ]
1823pub struct FlushableActivitySummary {
1924 pub network_activity_count : u32 ,
@@ -22,15 +27,15 @@ pub struct FlushableActivitySummary {
2227}
2328
2429pub struct ActivityAggregator {
25- rx : mpsc:: UnboundedReceiver < ActivityEvent > ,
30+ rx : mpsc:: Receiver < ActivityEvent > ,
2631 network_activity_count : u32 ,
2732 denied_action_count : u32 ,
2833 denials_by_group : HashMap < String , u32 > ,
2934 flush_interval_secs : u64 ,
3035}
3136
3237impl ActivityAggregator {
33- pub fn new ( rx : mpsc:: UnboundedReceiver < ActivityEvent > , flush_interval_secs : u64 ) -> Self {
38+ pub fn new ( rx : mpsc:: Receiver < ActivityEvent > , flush_interval_secs : u64 ) -> Self {
3439 Self {
3540 rx,
3641 network_activity_count : 0 ,
@@ -42,9 +47,17 @@ impl ActivityAggregator {
4247
4348 pub async fn run < F , Fut > ( mut self , flush_callback : F )
4449 where
45- F : Fn ( FlushableActivitySummary ) -> Fut ,
46- Fut : Future < Output = ( ) > ,
50+ F : Fn ( FlushableActivitySummary ) -> Fut + Send + ' static ,
51+ Fut : Future < Output = ( ) > + Send + ' static ,
4752 {
53+ let ( flush_tx, mut flush_rx) =
54+ mpsc:: channel :: < FlushableActivitySummary > ( ACTIVITY_FLUSH_QUEUE_CAPACITY ) ;
55+ tokio:: spawn ( async move {
56+ while let Some ( summary) = flush_rx. recv ( ) . await {
57+ flush_callback ( summary) . await ;
58+ }
59+ } ) ;
60+
4861 let mut flush_interval =
4962 tokio:: time:: interval ( std:: time:: Duration :: from_secs ( self . flush_interval_secs ) ) ;
5063 flush_interval. tick ( ) . await ;
@@ -56,7 +69,7 @@ impl ActivityAggregator {
5669 self . ingest( event) ;
5770 } else {
5871 if let Some ( summary) = self . drain( ) {
59- flush_callback ( summary) . await ;
72+ queue_flush_summary ( & flush_tx , summary) ;
6073 }
6174 debug!( "ActivityAggregator: channel closed, exiting" ) ;
6275 return ;
@@ -69,7 +82,7 @@ impl ActivityAggregator {
6982 denied = summary. denied_action_count,
7083 "ActivityAggregator: flushing anonymous activity summary"
7184 ) ;
72- flush_callback ( summary) . await ;
85+ queue_flush_summary ( & flush_tx , summary) ;
7386 }
7487 }
7588 }
@@ -103,6 +116,17 @@ impl ActivityAggregator {
103116 }
104117}
105118
119+ pub fn try_record_activity ( tx : & ActivitySender , denied : bool , deny_group : & ' static str ) -> bool {
120+ tx. try_send ( ActivityEvent { denied, deny_group } ) . is_ok ( )
121+ }
122+
123+ fn queue_flush_summary (
124+ tx : & mpsc:: Sender < FlushableActivitySummary > ,
125+ summary : FlushableActivitySummary ,
126+ ) -> bool {
127+ tx. try_send ( summary) . is_ok ( )
128+ }
129+
106130pub fn sanitize_deny_group ( raw : & str ) -> & ' static str {
107131 match raw {
108132 "connect_policy" | "connect" | "l4_deny" => "connect_policy" ,
@@ -150,4 +174,25 @@ mod tests {
150174 assert_float_eq ( denial_rate_pct ( 4 , 1 ) , 25.0 ) ;
151175 assert_float_eq ( denial_rate_pct ( 4 , 10 ) , 100.0 ) ;
152176 }
177+
178+ #[ test]
179+ fn activity_send_drops_when_queue_is_full ( ) {
180+ let ( tx, _rx) = mpsc:: channel ( 1 ) ;
181+
182+ assert ! ( try_record_activity( & tx, false , "unknown" ) ) ;
183+ assert ! ( !try_record_activity( & tx, true , "connect_policy" ) ) ;
184+ }
185+
186+ #[ test]
187+ fn flush_summary_drops_when_queue_is_full ( ) {
188+ let ( tx, _rx) = mpsc:: channel ( 1 ) ;
189+ let summary = FlushableActivitySummary {
190+ network_activity_count : 1 ,
191+ denied_action_count : 0 ,
192+ denials_by_group : Vec :: new ( ) ,
193+ } ;
194+
195+ assert ! ( queue_flush_summary( & tx, summary. clone( ) ) ) ;
196+ assert ! ( !queue_flush_summary( & tx, summary) ) ;
197+ }
153198}
0 commit comments