@@ -7,7 +7,7 @@ use std::sync::Arc;
77use std:: time:: Duration ;
88
99use axum:: extract:: ws:: { Message as WsMessage , WebSocket } ;
10- use futures_util:: { SinkExt , StreamExt } ;
10+ use futures_util:: { Sink , SinkExt , StreamExt } ;
1111use tokio:: sync:: { mpsc, Mutex , RwLock } ;
1212use tokio_util:: sync:: CancellationToken ;
1313use tracing:: Instrument as _;
@@ -29,6 +29,9 @@ const AUTH_TIMEOUT: Duration = Duration::from_secs(5);
2929/// Shared mutable subscription map for a single WebSocket connection.
3030pub ( crate ) type ConnectionSubscriptions = Arc < Mutex < HashMap < String , Vec < Filter > > > > ;
3131
32+ /// Maximum outbound data frames buffered into the websocket sink before one flush.
33+ const MAX_WS_SEND_BATCH : usize = 64 ;
34+
3235/// NIP-42 authentication state for a single connection.
3336#[ derive( Debug , Clone ) ]
3437pub enum AuthState {
@@ -262,11 +265,22 @@ pub async fn handle_connection(
262265/// is stalled, control frames queue in the small ctrl_rx buffer; callers
263266/// treat a full control channel as terminal (Bug 7 fix).
264267async fn send_loop (
265- mut ws_send : futures_util:: stream:: SplitSink < WebSocket , WsMessage > ,
268+ ws_send : futures_util:: stream:: SplitSink < WebSocket , WsMessage > ,
269+ data_rx : mpsc:: Receiver < WsMessage > ,
270+ ctrl_rx : mpsc:: Receiver < WsMessage > ,
271+ cancel : CancellationToken ,
272+ ) {
273+ send_loop_inner ( ws_send, data_rx, ctrl_rx, cancel) . await ;
274+ }
275+
276+ async fn send_loop_inner < S > (
277+ mut ws_send : S ,
266278 mut data_rx : mpsc:: Receiver < WsMessage > ,
267279 mut ctrl_rx : mpsc:: Receiver < WsMessage > ,
268280 cancel : CancellationToken ,
269- ) {
281+ ) where
282+ S : Sink < WsMessage > + Unpin ,
283+ {
270284 loop {
271285 // Priority: drain all pending control frames before data.
272286 while let Ok ( ctrl_msg) = ctrl_rx. try_recv ( ) {
@@ -289,9 +303,28 @@ async fn send_loop(
289303 }
290304 }
291305 Some ( msg) = data_rx. recv( ) => {
292- if ws_send. send( msg) . await . is_err( ) {
306+ let mut batched = 1usize ;
307+ if ws_send. feed( msg) . await . is_err( ) {
308+ break ;
309+ }
310+
311+ while batched < MAX_WS_SEND_BATCH {
312+ match data_rx. try_recv( ) {
313+ Ok ( next) => {
314+ if ws_send. feed( next) . await . is_err( ) {
315+ return ;
316+ }
317+ batched += 1 ;
318+ }
319+ Err ( tokio:: sync:: mpsc:: error:: TryRecvError :: Empty ) => break ,
320+ Err ( tokio:: sync:: mpsc:: error:: TryRecvError :: Disconnected ) => break ,
321+ }
322+ }
323+
324+ if ws_send. flush( ) . await . is_err( ) {
293325 break ;
294326 }
327+ metrics:: histogram!( "buzz_ws_send_batch_size" ) . record( batched as f64 ) ;
295328 }
296329 }
297330 }
@@ -512,3 +545,158 @@ fn topic_for_subscription(channel_id: Option<Uuid>) -> EventTopic {
512545 None => EventTopic :: Global ,
513546 }
514547}
548+
549+ #[ cfg( test) ]
550+ mod tests {
551+ use super :: * ;
552+ use std:: sync:: { Arc , Mutex } ;
553+
554+ #[ derive( Debug , Default ) ]
555+ struct MockSinkState {
556+ messages : Vec < WsMessage > ,
557+ flush_count : usize ,
558+ fail_after_flushes : Option < usize > ,
559+ }
560+
561+ #[ derive( Debug , Clone ) ]
562+ struct MockSink {
563+ state : Arc < Mutex < MockSinkState > > ,
564+ }
565+
566+ impl MockSink {
567+ fn new ( fail_after_flushes : Option < usize > ) -> ( Self , Arc < Mutex < MockSinkState > > ) {
568+ let state = Arc :: new ( Mutex :: new ( MockSinkState {
569+ fail_after_flushes,
570+ ..MockSinkState :: default ( )
571+ } ) ) ;
572+ (
573+ Self {
574+ state : Arc :: clone ( & state) ,
575+ } ,
576+ state,
577+ )
578+ }
579+ }
580+
581+ impl Sink < WsMessage > for MockSink {
582+ type Error = std:: io:: Error ;
583+
584+ fn poll_ready (
585+ self : std:: pin:: Pin < & mut Self > ,
586+ _cx : & mut std:: task:: Context < ' _ > ,
587+ ) -> std:: task:: Poll < Result < ( ) , Self :: Error > > {
588+ std:: task:: Poll :: Ready ( Ok ( ( ) ) )
589+ }
590+
591+ fn start_send ( self : std:: pin:: Pin < & mut Self > , item : WsMessage ) -> Result < ( ) , Self :: Error > {
592+ self . state
593+ . lock ( )
594+ . expect ( "mock sink poisoned" )
595+ . messages
596+ . push ( item) ;
597+ Ok ( ( ) )
598+ }
599+
600+ fn poll_flush (
601+ self : std:: pin:: Pin < & mut Self > ,
602+ _cx : & mut std:: task:: Context < ' _ > ,
603+ ) -> std:: task:: Poll < Result < ( ) , Self :: Error > > {
604+ let mut state = self . state . lock ( ) . expect ( "mock sink poisoned" ) ;
605+ state. flush_count += 1 ;
606+ if state
607+ . fail_after_flushes
608+ . is_some_and ( |limit| state. flush_count >= limit)
609+ {
610+ return std:: task:: Poll :: Ready ( Err ( std:: io:: Error :: new (
611+ std:: io:: ErrorKind :: BrokenPipe ,
612+ "mock flush failure" ,
613+ ) ) ) ;
614+ }
615+ std:: task:: Poll :: Ready ( Ok ( ( ) ) )
616+ }
617+
618+ fn poll_close (
619+ self : std:: pin:: Pin < & mut Self > ,
620+ cx : & mut std:: task:: Context < ' _ > ,
621+ ) -> std:: task:: Poll < Result < ( ) , Self :: Error > > {
622+ self . poll_flush ( cx)
623+ }
624+ }
625+
626+ fn text_payloads ( messages : & [ WsMessage ] ) -> Vec < String > {
627+ messages
628+ . iter ( )
629+ . map ( |msg| match msg {
630+ WsMessage :: Text ( text) => text. to_string ( ) ,
631+ other => panic ! ( "unexpected websocket message in test: {other:?}" ) ,
632+ } )
633+ . collect ( )
634+ }
635+
636+ #[ tokio:: test]
637+ async fn send_loop_batches_queued_data_frames_into_one_flush ( ) {
638+ let ( data_tx, data_rx) = mpsc:: channel ( MAX_WS_SEND_BATCH ) ;
639+ let ( _ctrl_tx, ctrl_rx) = mpsc:: channel ( 1 ) ;
640+ for i in 0 ..5 {
641+ data_tx
642+ . send ( WsMessage :: Text ( format ! ( "data-{i}" ) . into ( ) ) )
643+ . await
644+ . expect ( "queue data frame" ) ;
645+ }
646+
647+ let ( sink, state) = MockSink :: new ( Some ( 1 ) ) ;
648+ send_loop_inner ( sink, data_rx, ctrl_rx, CancellationToken :: new ( ) ) . await ;
649+
650+ let state = state. lock ( ) . expect ( "mock sink poisoned" ) ;
651+ assert_eq ! ( state. flush_count, 1 ) ;
652+ assert_eq ! (
653+ text_payloads( & state. messages) ,
654+ vec![ "data-0" , "data-1" , "data-2" , "data-3" , "data-4" ]
655+ ) ;
656+ }
657+
658+ #[ tokio:: test]
659+ async fn send_loop_batch_one_preserves_single_frame_flush_behavior ( ) {
660+ let ( data_tx, data_rx) = mpsc:: channel ( 1 ) ;
661+ let ( _ctrl_tx, ctrl_rx) = mpsc:: channel ( 1 ) ;
662+ data_tx
663+ . send ( WsMessage :: Text ( "single" . into ( ) ) )
664+ . await
665+ . expect ( "queue data frame" ) ;
666+
667+ let ( sink, state) = MockSink :: new ( Some ( 1 ) ) ;
668+ send_loop_inner ( sink, data_rx, ctrl_rx, CancellationToken :: new ( ) ) . await ;
669+
670+ let state = state. lock ( ) . expect ( "mock sink poisoned" ) ;
671+ assert_eq ! ( state. flush_count, 1 ) ;
672+ assert_eq ! ( text_payloads( & state. messages) , vec![ "single" ] ) ;
673+ }
674+
675+ #[ tokio:: test]
676+ async fn send_loop_drains_control_before_batched_data_without_reordering ( ) {
677+ let ( data_tx, data_rx) = mpsc:: channel ( MAX_WS_SEND_BATCH ) ;
678+ let ( ctrl_tx, ctrl_rx) = mpsc:: channel ( 1 ) ;
679+ data_tx
680+ . send ( WsMessage :: Text ( "data-0" . into ( ) ) )
681+ . await
682+ . expect ( "queue data frame" ) ;
683+ data_tx
684+ . send ( WsMessage :: Text ( "data-1" . into ( ) ) )
685+ . await
686+ . expect ( "queue data frame" ) ;
687+ ctrl_tx
688+ . send ( WsMessage :: Text ( "control" . into ( ) ) )
689+ . await
690+ . expect ( "queue control frame" ) ;
691+
692+ let ( sink, state) = MockSink :: new ( Some ( 2 ) ) ;
693+ send_loop_inner ( sink, data_rx, ctrl_rx, CancellationToken :: new ( ) ) . await ;
694+
695+ let state = state. lock ( ) . expect ( "mock sink poisoned" ) ;
696+ assert_eq ! ( state. flush_count, 2 ) ;
697+ assert_eq ! (
698+ text_payloads( & state. messages) ,
699+ vec![ "control" , "data-0" , "data-1" ]
700+ ) ;
701+ }
702+ }
0 commit comments