Skip to content

Commit 820329b

Browse files
committed
refactor: [#1264] return wether swarm stats have change or not after upserting a peer
When you upsert a new peer (announce requests) the swarm stats migth change if the peer announced that has completed the download. If that case we return `true` given the caller the opportunity to know if stats have changed. In the current implementation we get stats before and after upserintg the peer, and we compare them to check if they have changed. However stats migth have change due to a parallel (race condition) announce from another peer. This is the only way to know exactly if this announce request has altered the stats (incremented the number of downloads).
1 parent bf883da commit 820329b

File tree

20 files changed

+77
-68
lines changed

20 files changed

+77
-68
lines changed

packages/axum-http-tracker-server/src/environment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub struct Environment<S> {
2222
impl<S> Environment<S> {
2323
/// Add a torrent to the tracker
2424
pub fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
25-
let () = self
25+
let _stats_updated = self
2626
.container
2727
.tracker_core_container
2828
.in_memory_torrent_repository

packages/axum-rest-tracker-api-server/src/environment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
{
3434
/// Add a torrent to the tracker
3535
pub fn add_torrent_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
36-
let () = self
36+
let _stats_updated = self
3737
.container
3838
.tracker_core_container
3939
.in_memory_torrent_repository

packages/torrent-repository/src/entry/single.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ impl Entry for EntrySingle {
6666
}
6767
}
6868
_ => {
69+
// `Started` event (first announced event) or
70+
// `None` event (announcements done at regular intervals).
6971
drop(self.swarm.upsert(Arc::new(*peer)));
7072
}
7173
}

packages/torrent-repository/src/repository/dash_map_mutex_std.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ where
2323
EntryMutexStd: EntrySync,
2424
EntrySingle: Entry,
2525
{
26-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
26+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
2727
if let Some(entry) = self.torrents.get(info_hash) {
28-
entry.upsert_peer(peer);
28+
entry.upsert_peer(peer)
2929
} else {
3030
let _unused = self.torrents.insert(*info_hash, Arc::default());
3131
if let Some(entry) = self.torrents.get(info_hash) {
32-
entry.upsert_peer(peer);
32+
entry.upsert_peer(peer)
33+
} else {
34+
false
3335
}
3436
}
3537
}

packages/torrent-repository/src/repository/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub trait Repository<T>: Debug + Default + Sized + 'static {
2424
fn remove(&self, key: &InfoHash) -> Option<T>;
2525
fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch);
2626
fn remove_peerless_torrents(&self, policy: &TrackerPolicy);
27-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer);
27+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool;
2828
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata>;
2929
}
3030

@@ -37,6 +37,6 @@ pub trait RepositoryAsync<T>: Debug + Default + Sized + 'static {
3737
fn remove(&self, key: &InfoHash) -> impl std::future::Future<Output = Option<T>> + Send;
3838
fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) -> impl std::future::Future<Output = ()> + Send;
3939
fn remove_peerless_torrents(&self, policy: &TrackerPolicy) -> impl std::future::Future<Output = ()> + Send;
40-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> impl std::future::Future<Output = ()> + Send;
40+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> impl std::future::Future<Output = bool> + Send;
4141
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> impl std::future::Future<Output = Option<SwarmMetadata>> + Send;
4242
}

packages/torrent-repository/src/repository/rw_lock_std.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ impl Repository<EntrySingle> for TorrentsRwLockStd
4646
where
4747
EntrySingle: Entry,
4848
{
49-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
49+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
5050
let mut db = self.get_torrents_mut();
5151

5252
let entry = db.entry(*info_hash).or_insert(EntrySingle::default());
5353

54-
entry.upsert_peer(peer);
54+
entry.upsert_peer(peer)
5555
}
5656

5757
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {

packages/torrent-repository/src/repository/rw_lock_std_mutex_std.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ where
3333
EntryMutexStd: EntrySync,
3434
EntrySingle: Entry,
3535
{
36-
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
36+
fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
3737
let maybe_entry = self.get_torrents().get(info_hash).cloned();
3838

3939
let entry = if let Some(entry) = maybe_entry {
@@ -44,7 +44,7 @@ where
4444
entry.clone()
4545
};
4646

47-
entry.upsert_peer(peer);
47+
entry.upsert_peer(peer)
4848
}
4949

5050
fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {

packages/torrent-repository/src/repository/rw_lock_std_mutex_tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ where
3737
EntryMutexTokio: EntryAsync,
3838
EntrySingle: Entry,
3939
{
40-
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
40+
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
4141
let maybe_entry = self.get_torrents().get(info_hash).cloned();
4242

4343
let entry = if let Some(entry) = maybe_entry {
@@ -48,7 +48,7 @@ where
4848
entry.clone()
4949
};
5050

51-
entry.upsert_peer(peer).await;
51+
entry.upsert_peer(peer).await
5252
}
5353

5454
async fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {

packages/torrent-repository/src/repository/rw_lock_tokio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ impl RepositoryAsync<EntrySingle> for TorrentsRwLockTokio
4747
where
4848
EntrySingle: Entry,
4949
{
50-
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
50+
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
5151
let mut db = self.get_torrents_mut().await;
5252

5353
let entry = db.entry(*info_hash).or_insert(EntrySingle::default());
5454

55-
entry.upsert_peer(peer);
55+
entry.upsert_peer(peer)
5656
}
5757

5858
async fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {

packages/torrent-repository/src/repository/rw_lock_tokio_mutex_std.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ where
3535
EntryMutexStd: EntrySync,
3636
EntrySingle: Entry,
3737
{
38-
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) {
38+
async fn upsert_peer(&self, info_hash: &InfoHash, peer: &peer::Peer) -> bool {
3939
let maybe_entry = self.get_torrents().await.get(info_hash).cloned();
4040

4141
let entry = if let Some(entry) = maybe_entry {
@@ -46,7 +46,7 @@ where
4646
entry.clone()
4747
};
4848

49-
entry.upsert_peer(peer);
49+
entry.upsert_peer(peer)
5050
}
5151

5252
async fn get_swarm_metadata(&self, info_hash: &InfoHash) -> Option<SwarmMetadata> {

0 commit comments

Comments
 (0)