From c76d7442e2de13ac5805dff861e8d60316d67059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 6 Aug 2022 16:50:56 +0200 Subject: [PATCH 1/2] Use regular (ahash) IndexMap for PeerMaps --- Cargo.lock | 1 + aquatic_common/Cargo.toml | 1 + aquatic_common/src/lib.rs | 5 ++++- aquatic_http/src/workers/swarm.rs | 4 ++-- aquatic_http_private/src/workers/swarm/common.rs | 6 ++++-- aquatic_udp/src/workers/swarm/storage.rs | 3 ++- aquatic_ws/src/workers/swarm.rs | 4 ++-- 7 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f80d310..ef4554d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -68,6 +68,7 @@ dependencies = [ "hashbrown 0.12.3", "hex", "hwloc", + "indexmap", "indexmap-amortized", "libc", "log", diff --git a/aquatic_common/Cargo.toml b/aquatic_common/Cargo.toml index 293bffe..759cccb 100644 --- a/aquatic_common/Cargo.toml +++ b/aquatic_common/Cargo.toml @@ -24,6 +24,7 @@ duplicate = "0.4" git-testament = "0.2" hashbrown = "0.12" hex = "0.4" +indexmap = "1" indexmap-amortized = "1" libc = "0.2" log = "0.4" diff --git a/aquatic_common/src/lib.rs b/aquatic_common/src/lib.rs index b23a71b..7d51312 100644 --- a/aquatic_common/src/lib.rs +++ b/aquatic_common/src/lib.rs @@ -13,6 +13,9 @@ pub mod privileges; #[cfg(feature = "rustls")] pub mod rustls_config; +/// IndexMap using AHash hasher +pub type IndexMap = indexmap::IndexMap; + /// Amortized IndexMap using AHash hasher pub type AmortizedIndexMap = indexmap_amortized::IndexMap; @@ -104,7 +107,7 @@ impl Drop for PanicSentinel { #[inline] pub fn extract_response_peers( rng: &mut impl Rng, - peer_map: &AmortizedIndexMap, + peer_map: &IndexMap, max_num_peers_to_take: usize, sender_peer_map_key: K, peer_conversion_function: F, diff --git a/aquatic_http/src/workers/swarm.rs b/aquatic_http/src/workers/swarm.rs index cbfef5c..6e3c6c5 100644 --- a/aquatic_http/src/workers/swarm.rs +++ b/aquatic_http/src/workers/swarm.rs @@ -16,7 +16,7 @@ use rand::SeedableRng; use smartstring::{LazyCompact, SmartString}; use aquatic_common::access_list::{create_access_list_cache, AccessListArcSwap, AccessListCache}; -use aquatic_common::{extract_response_peers, PanicSentinel}; +use aquatic_common::{extract_response_peers, IndexMap, PanicSentinel}; use aquatic_common::{AmortizedIndexMap, CanonicalSocketAddr}; use aquatic_common::{SecondsSinceServerStart, ServerStartInstant, ValidUntil}; use aquatic_http_protocol::common::*; @@ -78,7 +78,7 @@ pub struct PeerMapKey { pub ip_or_key: Either>, } -pub type PeerMap = AmortizedIndexMap, Peer>; +pub type PeerMap = IndexMap, Peer>; pub struct TorrentData { pub peers: PeerMap, diff --git a/aquatic_http_private/src/workers/swarm/common.rs b/aquatic_http_private/src/workers/swarm/common.rs index 4fc6a12..277da34 100644 --- a/aquatic_http_private/src/workers/swarm/common.rs +++ b/aquatic_http_private/src/workers/swarm/common.rs @@ -1,6 +1,8 @@ use std::net::{Ipv4Addr, Ipv6Addr}; -use aquatic_common::{AmortizedIndexMap, SecondsSinceServerStart, ServerStartInstant, ValidUntil}; +use aquatic_common::{ + AmortizedIndexMap, IndexMap, SecondsSinceServerStart, ServerStartInstant, ValidUntil, +}; use aquatic_http_protocol::common::{AnnounceEvent, InfoHash, PeerId}; use aquatic_http_protocol::response::ResponsePeer; @@ -55,7 +57,7 @@ pub struct PeerMapKey { pub ip_address: I, } -pub type PeerMap = AmortizedIndexMap, Peer>; +pub type PeerMap = IndexMap, Peer>; pub struct TorrentData { pub peers: PeerMap, diff --git a/aquatic_udp/src/workers/swarm/storage.rs b/aquatic_udp/src/workers/swarm/storage.rs index 5870ebe..6523d25 100644 --- a/aquatic_udp/src/workers/swarm/storage.rs +++ b/aquatic_udp/src/workers/swarm/storage.rs @@ -2,6 +2,7 @@ use std::net::Ipv4Addr; use std::net::Ipv6Addr; use std::sync::Arc; +use aquatic_common::IndexMap; use aquatic_common::SecondsSinceServerStart; use aquatic_common::ServerStartInstant; use aquatic_common::{ @@ -35,7 +36,7 @@ impl Peer { } } -pub type PeerMap = AmortizedIndexMap>; +pub type PeerMap = IndexMap>; pub struct TorrentData { peers: PeerMap, diff --git a/aquatic_ws/src/workers/swarm.rs b/aquatic_ws/src/workers/swarm.rs index b8a06f7..b180fb8 100644 --- a/aquatic_ws/src/workers/swarm.rs +++ b/aquatic_ws/src/workers/swarm.rs @@ -13,7 +13,7 @@ use hashbrown::HashMap; use rand::{rngs::SmallRng, SeedableRng}; use aquatic_common::{ - extract_response_peers, AmortizedIndexMap, PanicSentinel, SecondsSinceServerStart, + extract_response_peers, AmortizedIndexMap, IndexMap, PanicSentinel, SecondsSinceServerStart, ServerStartInstant, }; use aquatic_ws_protocol::*; @@ -53,7 +53,7 @@ struct Peer { pub valid_until: ValidUntil, } -type PeerMap = AmortizedIndexMap; +type PeerMap = IndexMap; struct TorrentData { pub peers: PeerMap, From becf88c3721deeb77120630e5aca7808b16dcfd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 6 Aug 2022 18:14:12 +0200 Subject: [PATCH 2/2] udp: in Peer, replace PeerStatus with is_seeder bool --- aquatic_udp/src/workers/swarm/mod.rs | 19 +++---- aquatic_udp/src/workers/swarm/storage.rs | 63 +++++++++++++++--------- 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/aquatic_udp/src/workers/swarm/mod.rs b/aquatic_udp/src/workers/swarm/mod.rs index dbf2b03..2c054b3 100644 --- a/aquatic_udp/src/workers/swarm/mod.rs +++ b/aquatic_udp/src/workers/swarm/mod.rs @@ -17,7 +17,7 @@ use aquatic_udp_protocol::*; use crate::common::*; use crate::config::Config; -use storage::{Peer, TorrentMap, TorrentMaps}; +use storage::{TorrentMap, TorrentMaps}; pub fn run_swarm_worker( _sentinel: PanicSentinel, @@ -145,16 +145,17 @@ fn handle_announce_request( ) }; - let peer = Peer { - ip_address: peer_ip, - port: request.port, - status: PeerStatus::from_event_and_bytes_left(request.event, request.bytes_left), - valid_until: peer_valid_until, - }; - let torrent_data = torrents.0.entry(request.info_hash).or_default(); - torrent_data.update_peer(request.peer_id, peer); + let peer_status = PeerStatus::from_event_and_bytes_left(request.event, request.bytes_left); + + torrent_data.update_peer( + request.peer_id, + peer_ip, + request.port, + peer_status, + peer_valid_until, + ); let response_peers = torrent_data.extract_response_peers(rng, request.peer_id, max_num_peers_to_take); diff --git a/aquatic_udp/src/workers/swarm/storage.rs b/aquatic_udp/src/workers/swarm/storage.rs index 6523d25..cebe1df 100644 --- a/aquatic_udp/src/workers/swarm/storage.rs +++ b/aquatic_udp/src/workers/swarm/storage.rs @@ -20,15 +20,15 @@ use crate::config::Config; use super::create_torrent_scrape_statistics; #[derive(Clone, Debug)] -pub struct Peer { - pub ip_address: I, - pub port: Port, - pub status: PeerStatus, - pub valid_until: ValidUntil, +struct Peer { + ip_address: I, + port: Port, + is_seeder: bool, + valid_until: ValidUntil, } impl Peer { - pub fn to_response_peer(&self) -> ResponsePeer { + fn to_response_peer(&self) -> ResponsePeer { ResponsePeer { ip_address: self.ip_address, port: self.port, @@ -36,7 +36,7 @@ impl Peer { } } -pub type PeerMap = IndexMap>; +type PeerMap = IndexMap>; pub struct TorrentData { peers: PeerMap, @@ -45,14 +45,35 @@ pub struct TorrentData { } impl TorrentData { - pub fn update_peer(&mut self, peer_id: PeerId, peer: Peer) { - let opt_removed_peer = match peer.status { + pub fn update_peer( + &mut self, + peer_id: PeerId, + ip_address: I, + port: Port, + status: PeerStatus, + valid_until: ValidUntil, + ) { + let opt_removed_peer = match status { PeerStatus::Leeching => { + let peer = Peer { + ip_address, + port, + is_seeder: false, + valid_until, + }; + self.num_leechers += 1; self.peers.insert(peer_id, peer) } PeerStatus::Seeding => { + let peer = Peer { + ip_address, + port, + is_seeder: true, + valid_until, + }; + self.num_seeders += 1; self.peers.insert(peer_id, peer) @@ -60,14 +81,14 @@ impl TorrentData { PeerStatus::Stopped => self.peers.remove(&peer_id), }; - match opt_removed_peer.map(|peer| peer.status) { - Some(PeerStatus::Leeching) => { + match opt_removed_peer.map(|peer| peer.is_seeder) { + Some(true) => { self.num_leechers -= 1; } - Some(PeerStatus::Seeding) => { + Some(false) => { self.num_seeders -= 1; } - _ => {} + None => {} } } @@ -107,15 +128,11 @@ impl TorrentData { if peer.valid_until.valid(now) { true } else { - match peer.status { - PeerStatus::Seeding => { - self.num_seeders -= 1; - } - PeerStatus::Leeching => { - self.num_leechers -= 1; - } - _ => (), - }; + if peer.is_seeder { + self.num_seeders -= 1; + } else { + self.num_leechers -= 1; + } false } @@ -265,7 +282,7 @@ mod tests { Peer { ip_address: Ipv4Addr::from(i.to_be_bytes()), port: Port(1), - status: PeerStatus::Leeching, + is_seeder: false, valid_until: ValidUntil::new(ServerStartInstant::new(), 0), } }