Skip to content

Commit

Permalink
refactor: extract PeerList
Browse files Browse the repository at this point in the history
Extract a type for a collection of peers.

The performance adter the exatract is similar:

```output
Requests out: 415067.21/second
Responses in: 369397.08/second
  - Connect responses:  183049.81
  - Announce responses: 182717.15
  - Scrape responses:   3630.12
  - Error responses:    0.00
Peers per announce response: 0.00
Announce responses per info hash:
  - p10: 1
  - p25: 1
  - p50: 1
  - p75: 1
  - p90: 2
  - p95: 3
  - p99: 104
  - p99.9: 297
  - p100: 375
```
  • Loading branch information
josecelano committed Apr 15, 2024
1 parent 52b7e3a commit 4a567cd
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 54 deletions.
68 changes: 66 additions & 2 deletions packages/torrent-repository/src/entry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,72 @@ pub trait EntryAsync {
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Torrent {
/// The swarm: a network of peers that are all trying to download the torrent associated to this entry
// #[serde(skip)]
pub(crate) peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
pub(crate) peers: PeerList,
/// The number of peers that have ever completed downloading the torrent associated to this entry
pub(crate) downloaded: u32,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PeerList {
peers: std::collections::BTreeMap<peer::Id, Arc<peer::Peer>>,
}

impl PeerList {
fn len(&self) -> usize {
self.peers.len()
}

fn is_empty(&self) -> bool {
self.peers.is_empty()
}

fn insert(&mut self, key: peer::Id, value: Arc<peer::Peer>) -> Option<Arc<peer::Peer>> {
self.peers.insert(key, value)
}

fn remove(&mut self, key: &peer::Id) -> Option<Arc<peer::Peer>> {
self.peers.remove(key)
}

fn retain<F>(&mut self, f: F)
where
F: FnMut(&peer::Id, &mut Arc<peer::Peer>) -> bool,
{
self.peers.retain(f);
}

fn seeders_and_leechers(&self) -> (usize, usize) {
let seeders = self.peers.values().filter(|peer| peer.is_seeder()).count();
let leechers = self.len() - seeders;

(seeders, leechers)
}

fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self.peers.values().take(limit).cloned().collect(),
None => self.peers.values().cloned().collect(),
}
}

fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
// Limit the number of peers on the result
.take(limit)
.cloned()
.collect(),
None => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
.cloned()
.collect(),
}
}
}
32 changes: 6 additions & 26 deletions packages/torrent-repository/src/entry/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ use crate::EntrySingle;
impl Entry for EntrySingle {
#[allow(clippy::cast_possible_truncation)]
fn get_swarm_metadata(&self) -> SwarmMetadata {
let complete: u32 = self.peers.values().filter(|peer| peer.is_seeder()).count() as u32;
let incomplete: u32 = self.peers.len() as u32 - complete;
let (seeders, leechers) = self.peers.seeders_and_leechers();

SwarmMetadata {
downloaded: self.downloaded,
complete,
incomplete,
complete: seeders as u32,
incomplete: leechers as u32,
}
}

Expand All @@ -42,32 +41,13 @@ impl Entry for EntrySingle {
fn get_peers_len(&self) -> usize {
self.peers.len()
}

fn get_peers(&self, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self.peers.values().take(limit).cloned().collect(),
None => self.peers.values().cloned().collect(),
}
self.peers.get_peers(limit)
}

fn get_peers_for_client(&self, client: &SocketAddr, limit: Option<usize>) -> Vec<Arc<peer::Peer>> {
match limit {
Some(limit) => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
// Limit the number of peers on the result
.take(limit)
.cloned()
.collect(),
None => self
.peers
.values()
// Take peers which are not the client peer
.filter(|peer| peer::ReadInfo::get_address(peer.as_ref()) != *client)
.cloned()
.collect(),
}
self.peers.get_peers_for_client(client, limit)
}

fn upsert_peer(&mut self, peer: &peer::Peer) -> bool {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use dashmap::DashMap;
Expand All @@ -10,7 +9,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::{Entry, EntrySync};
use crate::entry::{Entry, EntrySync, PeerList};
use crate::{EntryMutexStd, EntrySingle};

#[derive(Default, Debug)]
Expand Down Expand Up @@ -82,7 +81,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
6 changes: 2 additions & 4 deletions packages/torrent-repository/src/repository/rw_lock_std.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::BTreeMap;

use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::pagination::Pagination;
Expand All @@ -8,7 +6,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::Entry;
use crate::entry::{Entry, PeerList};
use crate::{EntrySingle, TorrentsRwLockStd};

#[derive(Default, Debug)]
Expand Down Expand Up @@ -102,7 +100,7 @@ where
}

let entry = EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *downloaded,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
Expand All @@ -9,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::{Entry, EntrySync};
use crate::entry::{Entry, EntrySync, PeerList};
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockStdMutexStd};

impl TorrentsRwLockStdMutexStd {
Expand Down Expand Up @@ -97,7 +96,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::iter::zip;
use std::pin::Pin;
use std::sync::Arc;
Expand All @@ -13,7 +12,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::{Entry, EntryAsync};
use crate::entry::{Entry, EntryAsync, PeerList};
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockStdMutexTokio};

impl TorrentsRwLockStdMutexTokio {
Expand Down Expand Up @@ -106,7 +105,7 @@ where

let entry = EntryMutexTokio::new(
EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
6 changes: 2 additions & 4 deletions packages/torrent-repository/src/repository/rw_lock_tokio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::collections::BTreeMap;

use torrust_tracker_configuration::TrackerPolicy;
use torrust_tracker_primitives::info_hash::InfoHash;
use torrust_tracker_primitives::pagination::Pagination;
Expand All @@ -8,7 +6,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::Entry;
use crate::entry::{Entry, PeerList};
use crate::{EntrySingle, TorrentsRwLockTokio};

#[derive(Default, Debug)]
Expand Down Expand Up @@ -106,7 +104,7 @@ where
}

let entry = EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *completed,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
Expand All @@ -9,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::{Entry, EntrySync};
use crate::entry::{Entry, EntrySync, PeerList};
use crate::{EntryMutexStd, EntrySingle, TorrentsRwLockTokioMutexStd};

impl TorrentsRwLockTokioMutexStd {
Expand Down Expand Up @@ -97,7 +96,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use torrust_tracker_configuration::TrackerPolicy;
Expand All @@ -9,7 +8,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::RepositoryAsync;
use crate::entry::{Entry, EntryAsync};
use crate::entry::{Entry, EntryAsync, PeerList};
use crate::{EntryMutexTokio, EntrySingle, TorrentsRwLockTokioMutexTokio};

impl TorrentsRwLockTokioMutexTokio {
Expand Down Expand Up @@ -100,7 +99,7 @@ where

let entry = EntryMutexTokio::new(
EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::sync::Arc;

use crossbeam_skiplist::SkipMap;
Expand All @@ -10,7 +9,7 @@ use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch, PersistentTorrents};

use super::Repository;
use crate::entry::{Entry, EntrySync};
use crate::entry::{Entry, EntrySync, PeerList};
use crate::{EntryMutexStd, EntrySingle};

#[derive(Default, Debug)]
Expand Down Expand Up @@ -76,7 +75,7 @@ where

let entry = EntryMutexStd::new(
EntrySingle {
peers: BTreeMap::default(),
peers: PeerList::default(),
downloaded: *completed,
}
.into(),
Expand Down

0 comments on commit 4a567cd

Please sign in to comment.