@@ -541,7 +541,8 @@ async fn handle_text_message(text: String, conn: Arc<ConnectionState>, state: Ar
541541 let permit = match state. handler_semaphore . clone ( ) . try_acquire_owned ( ) {
542542 Ok ( p) => p,
543543 Err ( _) => {
544- conn. send ( RelayMessage :: notice (
544+ conn. send ( request_rejection_message (
545+ Some ( & sub_id) ,
545546 "rate-limited: too many concurrent requests" ,
546547 ) ) ;
547548 return ;
@@ -583,6 +584,13 @@ async fn handle_text_message(text: String, conn: Arc<ConnectionState>, state: Ar
583584 }
584585}
585586
587+ fn request_rejection_message ( sub_id : Option < & str > , reason : & str ) -> String {
588+ match sub_id {
589+ Some ( sub_id) => RelayMessage :: closed ( sub_id, reason) ,
590+ None => RelayMessage :: notice ( reason) ,
591+ }
592+ }
593+
586594async fn enforce_ws_admission (
587595 msg : & ClientMessage ,
588596 conn : & ConnectionState ,
@@ -613,7 +621,11 @@ async fn enforce_ws_admission(
613621 ws_limit,
614622 )
615623 . await ;
616- if !send_admission_result ( conn, ws_result) {
624+ let sub_id = match msg {
625+ ClientMessage :: Req { sub_id, .. } => Some ( sub_id. as_str ( ) ) ,
626+ _ => None ,
627+ } ;
628+ if !send_admission_result ( conn, ws_result, sub_id) {
617629 return false ;
618630 }
619631
@@ -632,7 +644,7 @@ async fn enforce_ws_admission(
632644 message_limit,
633645 )
634646 . await ;
635- if !send_admission_result ( conn, message_result) {
647+ if !send_admission_result ( conn, message_result, None ) {
636648 return false ;
637649 }
638650 }
@@ -643,19 +655,22 @@ async fn enforce_ws_admission(
643655fn send_admission_result (
644656 conn : & ConnectionState ,
645657 result : Result < ( ) , crate :: admission:: AdmissionError > ,
658+ sub_id : Option < & str > ,
646659) -> bool {
647660 match result {
648661 Ok ( ( ) ) => true ,
649662 Err ( crate :: admission:: AdmissionError :: Exceeded { reset_in_secs } ) => {
650663 metrics:: counter!( "buzz_admission_rejections_total" , "transport" => "websocket" , "reason" => "quota" ) . increment ( 1 ) ;
651- conn. send ( RelayMessage :: notice ( & format ! (
652- "rate-limited: quota exceeded; retry in {reset_in_secs}s"
653- ) ) ) ;
664+ conn. send ( request_rejection_message (
665+ sub_id,
666+ & format ! ( "rate-limited: quota exceeded; retry in {reset_in_secs}s" ) ,
667+ ) ) ;
654668 false
655669 }
656670 Err ( crate :: admission:: AdmissionError :: Unavailable ) => {
657671 metrics:: counter!( "buzz_admission_rejections_total" , "transport" => "websocket" , "reason" => "unavailable" ) . increment ( 1 ) ;
658- conn. send ( RelayMessage :: notice (
672+ conn. send ( request_rejection_message (
673+ sub_id,
659674 "rate-limited: shared admission unavailable" ,
660675 ) ) ;
661676 false
@@ -757,6 +772,19 @@ mod tests {
757772 . collect ( )
758773 }
759774
775+ #[ test]
776+ fn req_rejections_are_subscription_scoped ( ) {
777+ let reason = "rate-limited: too many concurrent requests" ;
778+ let closed: serde_json:: Value =
779+ serde_json:: from_str ( & request_rejection_message ( Some ( "history-123" ) , reason) )
780+ . expect ( "parse CLOSED" ) ;
781+ assert_eq ! ( closed, serde_json:: json!( [ "CLOSED" , "history-123" , reason] ) ) ;
782+
783+ let notice: serde_json:: Value =
784+ serde_json:: from_str ( & request_rejection_message ( None , reason) ) . expect ( "parse NOTICE" ) ;
785+ assert_eq ! ( notice, serde_json:: json!( [ "NOTICE" , reason] ) ) ;
786+ }
787+
760788 #[ tokio:: test]
761789 async fn send_loop_batches_queued_data_frames_into_one_flush ( ) {
762790 let ( data_tx, data_rx) = mpsc:: channel ( MAX_WS_SEND_BATCH ) ;
0 commit comments