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,10 @@ where
2070
2068
2071
2069
// remove topic from the peer_topics mapping
2072
2070
subscribed_topics. remove ( topic_hash) ;
2071
+ if let Some ( peers) = self . topic_peers . get_mut ( topic_hash) {
2072
+ peers. remove ( propagation_source) ;
2073
+ }
2074
+
2073
2075
unsubscribed_peers. push ( ( * propagation_source, topic_hash. clone ( ) ) ) ;
2074
2076
// generate an unsubscribe event to be polled
2075
2077
application_event. push ( ToSwarm :: GenerateEvent ( Event :: Unsubscribed {
@@ -2079,8 +2081,11 @@ where
2079
2081
}
2080
2082
}
2081
2083
2082
- if let Some ( m) = self . metrics . as_mut ( ) {
2083
- m. set_topic_peers ( topic_hash, peer_list. len ( ) ) ;
2084
+ if let ( Some ( m) , Some ( len) ) = (
2085
+ self . metrics . as_mut ( ) ,
2086
+ self . topic_peers . get ( topic_hash) . map ( |set| set. len ( ) ) ,
2087
+ ) {
2088
+ m. set_topic_peers ( topic_hash, len) ;
2084
2089
}
2085
2090
}
2086
2091
@@ -3337,6 +3342,11 @@ where
3337
3342
// support the protocol.
3338
3343
self . peer_topics . remove ( & peer_id) ;
3339
3344
3345
+ self . topic_peers . retain ( |_, peers| {
3346
+ peers. remove ( & peer_id) ;
3347
+ !peers. is_empty ( )
3348
+ } ) ;
3349
+
3340
3350
// If metrics are enabled, register the disconnection of a peer based on its protocol.
3341
3351
if let Some ( metrics) = self . metrics . as_mut ( ) {
3342
3352
let peer_kind = & self
0 commit comments