11//! EVENT handler — WS dispatcher → ingest pipeline → fan-out.
22
3- use std:: sync:: Arc ;
3+ use std:: { collections :: HashMap , sync:: Arc } ;
44
5+ use axum:: body:: Bytes ;
56use tracing:: { debug, error, info, warn} ;
67
78use buzz_core:: event:: StoredEvent ;
@@ -50,6 +51,50 @@ fn bounded_kind_label(kind: u32) -> String {
5051 }
5152}
5253
54+ fn event_frame_for_sub ( sub_id : & str , event_json : & str ) -> String {
55+ format ! ( r#"["EVENT","{}",{}]"# , sub_id, event_json)
56+ }
57+
58+ fn event_frame_bytes_for_sub ( sub_id : & str , event_json : & str ) -> Arc < Bytes > {
59+ Arc :: new ( Bytes :: from ( event_frame_for_sub ( sub_id, event_json) ) )
60+ }
61+
62+ fn fanout_frame_cache < ' a , I > ( sub_ids : I , event_json : & str ) -> HashMap < & ' a str , Arc < Bytes > >
63+ where
64+ I : IntoIterator < Item = & ' a str > ,
65+ {
66+ let mut frames = HashMap :: new ( ) ;
67+ for sub_id in sub_ids {
68+ frames
69+ . entry ( sub_id)
70+ . or_insert_with ( || event_frame_bytes_for_sub ( sub_id, event_json) ) ;
71+ }
72+ frames
73+ }
74+
75+ fn send_fanout_frames < ' a , I > (
76+ state : & AppState ,
77+ recipients : I ,
78+ frames : & HashMap < & ' a str , Arc < Bytes > > ,
79+ ) -> u32
80+ where
81+ I : IntoIterator < Item = ( crate :: subscription:: ConnId , & ' a str ) > ,
82+ {
83+ let mut drop_count = 0u32 ;
84+ for ( conn_id, sub_id) in recipients {
85+ let frame = frames
86+ . get ( sub_id)
87+ . expect ( "fan-out frame cache covers every recipient subscription id" ) ;
88+ if !state
89+ . conn_manager
90+ . send_to_text_bytes ( conn_id, Arc :: clone ( frame) )
91+ {
92+ drop_count += 1 ;
93+ }
94+ }
95+ drop_count
96+ }
97+
5398/// Drop recipients without access before fan-out on a private channel.
5499///
55100/// Open and channel-less events skip membership filtering (open channel-scoped
@@ -188,13 +233,17 @@ pub(crate) async fn fan_out_event_to_local_subscribers(
188233 return ;
189234 }
190235 } ;
191- let mut drop_count = 0u32 ;
192- for ( conn_id, sub_id) in & matches {
193- let msg = format ! ( r#"["EVENT","{}",{}]"# , sub_id, event_json) ;
194- if !state. conn_manager . send_to ( * conn_id, msg) {
195- drop_count += 1 ;
196- }
197- }
236+ let frames = fanout_frame_cache (
237+ matches. iter ( ) . map ( |( _, sub_id) | sub_id. as_str ( ) ) ,
238+ & event_json,
239+ ) ;
240+ let drop_count = send_fanout_frames (
241+ state,
242+ matches
243+ . iter ( )
244+ . map ( |( conn_id, sub_id) | ( * conn_id, sub_id. as_str ( ) ) ) ,
245+ & frames,
246+ ) ;
198247 if drop_count > 0 {
199248 tracing:: warn!(
200249 event_id = %stored. event. id. to_hex( ) ,
@@ -244,13 +293,17 @@ pub async fn fan_out_pubsub_event(state: &Arc<AppState>, channel_event: buzz_pub
244293 return ;
245294 }
246295 } ;
247- let mut drop_count = 0u32 ;
248- for ( conn_id, sub_id) in & matches {
249- let msg = format ! ( r#"["EVENT","{}",{}]"# , sub_id, event_json) ;
250- if !state. conn_manager . send_to ( * conn_id, msg) {
251- drop_count += 1 ;
252- }
253- }
296+ let frames = fanout_frame_cache (
297+ matches. iter ( ) . map ( |( _, sub_id) | sub_id. as_str ( ) ) ,
298+ & event_json,
299+ ) ;
300+ let drop_count = send_fanout_frames (
301+ state,
302+ matches
303+ . iter ( )
304+ . map ( |( conn_id, sub_id) | ( * conn_id, sub_id. as_str ( ) ) ) ,
305+ & frames,
306+ ) ;
254307 if drop_count > 0 {
255308 tracing:: warn!(
256309 event_id = %stored. event. id. to_hex( ) ,
@@ -391,23 +444,26 @@ async fn dispatch_persistent_event_inner(
391444 } )
392445 . flatten ( ) ;
393446 // Author-only delivery gating (NIP-ER reminders) is enforced centrally in
394- // filter_fanout_by_access, applied to `matches` above before this loop.
395- let mut drop_count = 0u32 ;
396- for ( target_conn_id, sub_id) in & matches {
397- if let Some ( ref owner_hex) = dm_visibility_owner {
398- let is_owner = state
399- . conn_manager
400- . pubkey_for ( * target_conn_id)
401- . is_some_and ( |pk| hex:: encode ( pk) == * owner_hex) ;
402- if !is_owner {
403- continue ;
447+ // filter_fanout_by_access, applied to `matches` above before this loop. The
448+ // DM visibility owner gate is an additional delivery fence, so build shared
449+ // frames only after applying it to the already access-filtered recipient set.
450+ let recipients: Vec < _ > = matches
451+ . iter ( )
452+ . filter_map ( |( target_conn_id, sub_id) | {
453+ if let Some ( ref owner_hex) = dm_visibility_owner {
454+ let is_owner = state
455+ . conn_manager
456+ . pubkey_for ( * target_conn_id)
457+ . is_some_and ( |pk| hex:: encode ( pk) == * owner_hex) ;
458+ if !is_owner {
459+ return None ;
460+ }
404461 }
405- }
406- let msg = format ! ( r#"["EVENT","{}",{}]"# , sub_id, event_json) ;
407- if !state. conn_manager . send_to ( * target_conn_id, msg) {
408- drop_count += 1 ;
409- }
410- }
462+ Some ( ( * target_conn_id, sub_id. as_str ( ) ) )
463+ } )
464+ . collect ( ) ;
465+ let frames = fanout_frame_cache ( recipients. iter ( ) . map ( |( _, sub_id) | * sub_id) , & event_json) ;
466+ let drop_count = send_fanout_frames ( state, recipients, & frames) ;
411467 if drop_count > 0 {
412468 tracing:: warn!(
413469 event_id = %event_id_hex,
@@ -1094,6 +1150,8 @@ fn single_tag_content<'a>(event: &'a Event, tag_name: &str) -> Result<&'a str, S
10941150
10951151#[ cfg( test) ]
10961152mod tests {
1153+ use std:: sync:: Arc ;
1154+
10971155 use buzz_core:: kind:: {
10981156 KIND_AGENT_OBSERVER_FRAME , KIND_CANVAS , KIND_FORUM_COMMENT , KIND_FORUM_POST ,
10991157 KIND_FORUM_VOTE , KIND_PRESENCE_UPDATE , KIND_STREAM_MESSAGE , KIND_STREAM_MESSAGE_DIFF ,
@@ -1104,6 +1162,42 @@ mod tests {
11041162 } ;
11051163 use nostr:: { EventBuilder , Keys , Kind , Tag } ;
11061164
1165+ #[ test]
1166+ fn fanout_event_frame_matches_legacy_format_byte_for_byte ( ) {
1167+ let sub_id = "sub-id" ;
1168+ let event_json = r#"{"id":"abc","tags":[["p","target"]],"content":"hello"}"# ;
1169+ let expected = format ! ( r#"["EVENT","{}",{}]"# , sub_id, event_json) ;
1170+
1171+ assert_eq ! ( super :: event_frame_for_sub( sub_id, event_json) , expected) ;
1172+ assert_eq ! (
1173+ super :: event_frame_bytes_for_sub( sub_id, event_json)
1174+ . as_ref( )
1175+ . as_ref( ) ,
1176+ expected. as_bytes( )
1177+ ) ;
1178+ }
1179+
1180+ #[ test]
1181+ fn fanout_frame_cache_reuses_frames_within_one_cycle_only ( ) {
1182+ let event_json = r#"{"id":"abc"}"# ;
1183+ let frames = super :: fanout_frame_cache ( [ "same" , "other" , "same" ] , event_json) ;
1184+
1185+ assert_eq ! ( frames. len( ) , 2 , "duplicate sub ids share one cached frame" ) ;
1186+ assert_eq ! (
1187+ frames. get( "same" ) . expect( "same frame" ) . as_ref( ) . as_ref( ) ,
1188+ format!( r#"["EVENT","same",{}]"# , event_json) . as_bytes( )
1189+ ) ;
1190+
1191+ let next_cycle = super :: fanout_frame_cache ( [ "same" ] , event_json) ;
1192+ assert ! (
1193+ !Arc :: ptr_eq(
1194+ frames. get( "same" ) . expect( "same frame" ) ,
1195+ next_cycle. get( "same" ) . expect( "same frame in next cycle" )
1196+ ) ,
1197+ "fan-out frame sharing must not escape a single cycle"
1198+ ) ;
1199+ }
1200+
11071201 #[ test]
11081202 fn channel_scoped_content_kinds_require_h_tags ( ) {
11091203 for kind in [
0 commit comments