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