From 78266fd3e7dbf50527b920b1ac3701b99e0f8b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 16 Apr 2022 00:52:34 +0200 Subject: [PATCH] udp: move some TorrentMap cleaning code to TorrentData impl --- aquatic_udp/src/workers/request/storage.rs | 67 +++++++++++----------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/aquatic_udp/src/workers/request/storage.rs b/aquatic_udp/src/workers/request/storage.rs index 05052ba..b5eaafd 100644 --- a/aquatic_udp/src/workers/request/storage.rs +++ b/aquatic_udp/src/workers/request/storage.rs @@ -38,6 +38,36 @@ pub struct TorrentData { pub num_leechers: usize, } +impl TorrentData { + fn clean_and_check_if_has_peers(&mut self, now: Instant) -> bool { + self.peers.retain(|_, peer| { + if peer.valid_until.0 > now { + true + } else { + match peer.status { + PeerStatus::Seeding => { + self.num_seeders -= 1; + } + PeerStatus::Leeching => { + self.num_leechers -= 1; + } + _ => (), + }; + + false + } + }); + + if self.peers.is_empty() { + false + } else { + self.peers.shrink_to_fit(); + + true + } + } +} + impl Default for TorrentData { fn default() -> Self { Self { @@ -68,7 +98,7 @@ impl TorrentMaps { access_list_cache .load() .allows(access_list_mode, &info_hash.0) - && Self::clean_torrent_and_peers(now, torrent) + && torrent.clean_and_check_if_has_peers(now) }); self.ipv4.shrink_to_fit(); @@ -76,41 +106,8 @@ impl TorrentMaps { access_list_cache .load() .allows(access_list_mode, &info_hash.0) - && Self::clean_torrent_and_peers(now, torrent) + && torrent.clean_and_check_if_has_peers(now) }); self.ipv6.shrink_to_fit(); } - - /// Returns true if torrent is to be kept - #[inline] - fn clean_torrent_and_peers(now: Instant, torrent: &mut TorrentData) -> bool { - let num_seeders = &mut torrent.num_seeders; - let num_leechers = &mut torrent.num_leechers; - - torrent.peers.retain(|_, peer| { - if peer.valid_until.0 > now { - true - } else { - match peer.status { - PeerStatus::Seeding => { - *num_seeders -= 1; - } - PeerStatus::Leeching => { - *num_leechers -= 1; - } - _ => (), - }; - - false - } - }); - - if torrent.peers.is_empty() { - false - } else { - torrent.peers.shrink_to_fit(); - - true - } - } }