@@ -155,7 +155,7 @@ pub struct QuerySubscription {
155
155
156
156
pub struct ChangeSubscription {
157
157
entries : HashMap < i32 , HashSet < Field > > ,
158
- sender : mpsc:: Sender < EntryUpdates > ,
158
+ sender : Option < mpsc:: Sender < EntryUpdates > > ,
159
159
permissions : Permissions ,
160
160
}
161
161
@@ -607,7 +607,7 @@ impl Subscriptions {
607
607
}
608
608
609
609
pub async fn notify (
610
- & self ,
610
+ & mut self ,
611
611
changed : Option < & HashMap < i32 , HashSet < Field > > > ,
612
612
db : & Database ,
613
613
) -> Result < Option < HashMap < String , ( ) > > , NotificationError > {
@@ -627,7 +627,7 @@ impl Subscriptions {
627
627
}
628
628
}
629
629
630
- for sub in & self . change_subscriptions {
630
+ for sub in & mut self . change_subscriptions {
631
631
match sub. notify ( changed, db) . await {
632
632
Ok ( _) => { }
633
633
Err ( err) => error = Some ( err) ,
@@ -660,12 +660,24 @@ impl Subscriptions {
660
660
true
661
661
}
662
662
} ) ;
663
- self . change_subscriptions . retain ( |sub| {
664
- if sub. sender . is_closed ( ) {
663
+ self . change_subscriptions . retain_mut ( |sub| {
664
+ if let Some ( sender) = & sub. sender {
665
+ if sender. is_closed ( ) {
666
+ info ! ( "Subscriber gone: removing subscription" ) ;
667
+ false
668
+ } else {
669
+ match & sub. permissions . expired ( ) {
670
+ Ok ( ( ) ) => true ,
671
+ Err ( PermissionError :: Expired ) => {
672
+ sub. sender = None ;
673
+ false
674
+ }
675
+ Err ( err) => panic ! ( "Error: {:?}" , err) ,
676
+ }
677
+ }
678
+ } else {
665
679
info ! ( "Subscriber gone: removing subscription" ) ;
666
680
false
667
- } else {
668
- true
669
681
}
670
682
} ) ;
671
683
}
@@ -693,7 +705,7 @@ impl ChangeSubscription {
693
705
// notify
694
706
let notifications = {
695
707
let mut notifications = EntryUpdates :: default ( ) ;
696
-
708
+ let mut error = None ;
697
709
for ( id, changed_fields) in changed {
698
710
if let Some ( fields) = self . entries . get ( id) {
699
711
if !fields. is_disjoint ( changed_fields) {
@@ -723,22 +735,32 @@ impl ChangeSubscription {
723
735
fields : notify_fields,
724
736
} ) ;
725
737
}
738
+ Err ( ReadError :: PermissionExpired ) => {
739
+ debug ! ( "notify: token expired, closing subscription channel" ) ;
740
+ error = Some ( NotificationError { } ) ;
741
+ break ;
742
+ }
726
743
Err ( _) => {
727
- debug ! ( "notify: could not find entry with id {}" , id)
744
+ debug ! ( "notify: could not find entry with id {}" , id) ;
728
745
}
729
746
}
730
747
}
731
748
}
732
749
}
750
+ if let Some ( err) = error {
751
+ return Err ( err) ;
752
+ }
733
753
notifications
734
754
} ;
735
755
if notifications. updates . is_empty ( ) {
736
756
Ok ( ( ) )
737
- } else {
738
- match self . sender . send ( notifications) . await {
757
+ } else if let Some ( sender ) = & self . sender {
758
+ match sender. send ( notifications) . await {
739
759
Ok ( ( ) ) => Ok ( ( ) ) ,
740
760
Err ( _) => Err ( NotificationError { } ) ,
741
761
}
762
+ } else {
763
+ Err ( NotificationError { } )
742
764
}
743
765
} else {
744
766
Ok ( ( ) )
@@ -775,9 +797,13 @@ impl ChangeSubscription {
775
797
}
776
798
notifications
777
799
} ;
778
- match self . sender . send ( notifications) . await {
779
- Ok ( ( ) ) => Ok ( ( ) ) ,
780
- Err ( _) => Err ( NotificationError { } ) ,
800
+ if let Some ( sender) = & self . sender {
801
+ match sender. send ( notifications) . await {
802
+ Ok ( ( ) ) => Ok ( ( ) ) ,
803
+ Err ( _) => Err ( NotificationError { } ) ,
804
+ }
805
+ } else {
806
+ Err ( NotificationError { } )
781
807
}
782
808
}
783
809
}
@@ -1411,7 +1437,7 @@ impl<'a, 'b> AuthorizedAccess<'a, 'b> {
1411
1437
match self
1412
1438
. broker
1413
1439
. subscriptions
1414
- . read ( )
1440
+ . write ( )
1415
1441
. await
1416
1442
. notify ( Some ( & changed) , & db)
1417
1443
. await
@@ -1457,7 +1483,7 @@ impl<'a, 'b> AuthorizedAccess<'a, 'b> {
1457
1483
let ( sender, receiver) = mpsc:: channel ( 10 ) ;
1458
1484
let subscription = ChangeSubscription {
1459
1485
entries : valid_entries,
1460
- sender,
1486
+ sender : Some ( sender ) ,
1461
1487
permissions : self . permissions . clone ( ) ,
1462
1488
} ;
1463
1489
0 commit comments