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