@@ -292,10 +292,14 @@ pub(crate) async fn handle_req(
292292 return ;
293293 }
294294
295+ let subscription_scope = authorized_requested_channels
296+ . clone ( )
297+ . map ( crate :: subscription:: SubscriptionScope :: Channels )
298+ . unwrap_or ( crate :: subscription:: SubscriptionScope :: Global ) ;
295299 if !register_subscription_if_current (
296300 & sub_id,
297301 & filters,
298- authorized_requested_channels . as_deref ( ) ,
302+ & subscription_scope ,
299303 & conn,
300304 & state. sub_registry ,
301305 state. pubsub . as_ref ( ) ,
@@ -305,7 +309,6 @@ pub(crate) async fn handle_req(
305309 {
306310 return ;
307311 }
308- }
309312
310313 debug ! ( conn_id = %conn_id, sub_id = %sub_id, "Subscription registered" ) ;
311314
@@ -498,7 +501,7 @@ const SEARCH_PAGE_SIZE: u32 = 100;
498501async fn register_subscription_if_current (
499502 sub_id : & str ,
500503 filters : & [ Filter ] ,
501- channel_id : Option < uuid :: Uuid > ,
504+ scope : & crate :: subscription :: SubscriptionScope ,
502505 conn : & ConnectionState ,
503506 registry : & crate :: subscription:: SubscriptionRegistry ,
504507 pubsub : & dyn SubscriptionTopics ,
@@ -517,21 +520,38 @@ async fn register_subscription_if_current(
517520 . await
518521 . insert ( sub_id. to_owned ( ) , filters. to_vec ( ) ) ;
519522
520- let replaced = registry. register_scoped (
521- conn. tenant . community ( ) ,
522- conn. conn_id ,
523- sub_id. to_owned ( ) ,
524- filters. to_vec ( ) ,
525- channel_id,
526- ) ;
523+ let replaced = match scope {
524+ crate :: subscription:: SubscriptionScope :: Channels ( channel_ids) => registry
525+ . register_channels_scoped (
526+ conn. tenant . community ( ) ,
527+ conn. conn_id ,
528+ sub_id. to_owned ( ) ,
529+ filters. to_vec ( ) ,
530+ channel_ids. clone ( ) ,
531+ ) ,
532+ crate :: subscription:: SubscriptionScope :: Global => registry. register_scoped (
533+ conn. tenant . community ( ) ,
534+ conn. conn_id ,
535+ sub_id. to_owned ( ) ,
536+ filters. to_vec ( ) ,
537+ None ,
538+ ) ,
539+ } ;
527540 if let Some ( replaced) = replaced {
528- pubsub
529- . release_topic ( & conn. tenant , topic_for_subscription ( replaced. channel_id ) )
530- . await ;
541+ release_subscription_topics ( pubsub, & conn. tenant , & replaced. scope ) . await ;
542+ }
543+ match scope {
544+ crate :: subscription:: SubscriptionScope :: Channels ( channel_ids) => {
545+ for & channel_id in channel_ids {
546+ pubsub
547+ . retain_topic ( & conn. tenant , EventTopic :: Channel ( channel_id) )
548+ . await ;
549+ }
550+ }
551+ crate :: subscription:: SubscriptionScope :: Global => {
552+ pubsub. retain_topic ( & conn. tenant , EventTopic :: Global ) . await ;
553+ }
531554 }
532- pubsub
533- . retain_topic ( & conn. tenant , topic_for_subscription ( channel_id) )
534- . await ;
535555
536556 // Only a successful replacement commit supersedes historical delivery from
537557 // the previous same-ID REQ. Validation/search failures leave it untouched.
@@ -1204,16 +1224,15 @@ pub(crate) fn extract_channel_ids_from_filters(filters: &[Filter]) -> Option<Vec
12041224}
12051225
12061226async fn release_subscription_topics (
1207- state : & AppState ,
1227+ pubsub : & dyn SubscriptionTopics ,
12081228 tenant : & TenantContext ,
12091229 scope : & crate :: subscription:: SubscriptionScope ,
12101230) {
12111231 if scope. is_global ( ) {
1212- state . pubsub . release_topic ( tenant, EventTopic :: Global ) . await ;
1232+ pubsub. release_topic ( tenant, EventTopic :: Global ) . await ;
12131233 } else {
12141234 for & channel_id in scope. channel_ids ( ) {
1215- state
1216- . pubsub
1235+ pubsub
12171236 . release_topic ( tenant, EventTopic :: Channel ( channel_id) )
12181237 . await ;
12191238 }
@@ -1618,7 +1637,7 @@ mod tests {
16181637 register_subscription_if_current (
16191638 & sub_id,
16201639 & filters,
1621- Some ( channel_id) ,
1640+ & crate :: subscription :: SubscriptionScope :: Channels ( vec ! [ channel_id] ) ,
16221641 & conn,
16231642 & registry,
16241643 pubsub. as_ref ( ) ,
@@ -1696,7 +1715,7 @@ mod tests {
16961715 register_subscription_if_current(
16971716 sub_id,
16981717 & filters,
1699- Some ( channel_id) ,
1718+ & crate :: subscription :: SubscriptionScope :: Channels ( vec! [ channel_id] ) ,
17001719 & conn,
17011720 & registry,
17021721 pubsub. as_ref( ) ,
@@ -1775,7 +1794,7 @@ mod tests {
17751794 let _ = register_subscription_if_current (
17761795 & sub_id,
17771796 & filters,
1778- Some ( channel_id) ,
1797+ & crate :: subscription :: SubscriptionScope :: Channels ( vec ! [ channel_id] ) ,
17791798 & conn,
17801799 & registry,
17811800 pubsub. as_ref ( ) ,
@@ -1790,8 +1809,7 @@ mod tests {
17901809 . expect ( "REQ reached pre-registration await" ) ;
17911810 crate :: connection:: shutdown_pending_requests ( & conn, & mut req_tasks) . await ;
17921811 for removed in registry. remove_connection ( conn. conn_id ) {
1793- pubsub
1794- . release_topic ( & conn. tenant , topic_for_subscription ( removed. channel_id ) )
1812+ release_subscription_topics ( pubsub. as_ref ( ) , & conn. tenant , & removed. scope )
17951813 . await ;
17961814 }
17971815 gate. notify_one ( ) ;
0 commit comments