Counting peers on drop

This commit is contained in:
Igor Katson 2024-08-21 11:31:36 +01:00
parent 67afdb0aa5
commit 0fdf6ad429
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
2 changed files with 14 additions and 0 deletions

View file

@ -91,6 +91,12 @@ impl PeerStateNoMut {
self.set(Default::default(), counters)
}
pub fn destroy(self, counters: &[&AggregatePeerStatsAtomic]) {
for counter in counters {
counter.dec(&self.0);
}
}
pub fn set(&mut self, new: PeerState, counters: &[&AggregatePeerStatsAtomic]) -> PeerState {
for counter in counters {
counter.incdec(&self.0, &new);

View file

@ -25,6 +25,14 @@ pub(crate) struct PeerStates {
pub states: DashMap<PeerHandle, Peer>,
}
impl Drop for PeerStates {
fn drop(&mut self) {
for (_, p) in std::mem::take(&mut self.states).into_iter() {
p.state.destroy(&[&self.session_stats.peers]);
}
}
}
impl PeerStates {
pub fn stats(&self) -> AggregatePeerStats {
AggregatePeerStats::from(&self.stats)