20
20
21
21
use std:: {
22
22
cmp:: { max, Ordering } ,
23
- collections:: HashSet ,
24
- collections:: { hash_map:: Entry , VecDeque } ,
25
- collections:: { BTreeSet , HashMap } ,
23
+ collections:: { hash_map:: Entry , BTreeSet , HashMap , HashSet , VecDeque } ,
26
24
fmt,
27
25
net:: IpAddr ,
28
26
task:: { Context , Poll } ,
@@ -2070,6 +2068,7 @@ where
2070
2068
2071
2069
// remove topic from the peer_topics mapping
2072
2070
subscribed_topics. remove ( topic_hash) ;
2071
+
2073
2072
unsubscribed_peers. push ( ( * propagation_source, topic_hash. clone ( ) ) ) ;
2074
2073
// generate an unsubscribe event to be polled
2075
2074
application_event. push ( ToSwarm :: GenerateEvent ( Event :: Unsubscribed {
@@ -2079,8 +2078,13 @@ where
2079
2078
}
2080
2079
}
2081
2080
2081
+ let peer_list_len = peer_list. len ( ) ;
2082
+ if peer_list_len == 0 {
2083
+ self . topic_peers . remove ( topic_hash) ;
2084
+ }
2085
+
2082
2086
if let Some ( m) = self . metrics . as_mut ( ) {
2083
- m. set_topic_peers ( topic_hash, peer_list . len ( ) ) ;
2087
+ m. set_topic_peers ( topic_hash, peer_list_len ) ;
2084
2088
}
2085
2089
}
2086
2090
@@ -3337,6 +3341,11 @@ where
3337
3341
// support the protocol.
3338
3342
self . peer_topics . remove ( & peer_id) ;
3339
3343
3344
+ self . topic_peers . retain ( |_, peers| {
3345
+ peers. remove ( & peer_id) ;
3346
+ !peers. is_empty ( )
3347
+ } ) ;
3348
+
3340
3349
// If metrics are enabled, register the disconnection of a peer based on its protocol.
3341
3350
if let Some ( metrics) = self . metrics . as_mut ( ) {
3342
3351
let peer_kind = & self
0 commit comments