Skip to content

Conversation

@josecelano
Copy link
Member

@josecelano josecelano commented May 5, 2025

Partially implements #1495

Improve torrent-repository package before adding events.

Sutasks

  • Update README

Refactoring PeerList

  • Rename PeerList to Swarm.
  • Use SocketAddr as key for peers in Swarm instead of the PeerId.
  • Use a cache for seeders and leechers counters to avoid iterating the list.
  • Move logic from TrackerTorrent to Swarm

Refactoring TrackedTorrent (it was removed)

  • Remove the downloaded field from TrackedTorrent. The logic can be moved to Swarm.
  • Document meets_retaining_policy method behavior. I don't know why peerless torrents are kept when persisted stats (downloaded field) are enabled (only if the torrent has been downloaded at least once).

It was removed because after the refactorings, it became just a wrapper over Swarm.

Refactoring TorrentRepository

To be implemented in a new PR.

New state

There are only two primary types:

  • Swarm: a collection of peers. Peers are members of the swarm.
  • Swarms: a collection of swarms.
$ tree ./src/
./src/
├── lib.rs
├── swarm.rs
└── swarms.rs

1 directory, 3 files

@josecelano josecelano self-assigned this May 5, 2025
@josecelano josecelano added the Code Cleanup / Refactoring Tidying and Making Neat label May 5, 2025
@codecov
Copy link

codecov bot commented May 5, 2025

Codecov Report

Attention: Patch coverage is 98.83527% with 7 lines in your changes missing coverage. Please review.

Project coverage is 85.06%. Comparing base (e1fd309) to head (728de22).
Report is 21 commits behind head on develop.

Files with missing lines Patch % Lines
packages/primitives/src/swarm_metadata.rs 33.33% 6 Missing ⚠️
packages/torrent-repository/src/swarms.rs 98.11% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1497      +/-   ##
===========================================
+ Coverage    84.83%   85.06%   +0.23%     
===========================================
  Files          259      258       -1     
  Lines        20287    20546     +259     
  Branches     20287    20546     +259     
===========================================
+ Hits         17210    17477     +267     
+ Misses        2788     2781       -7     
+ Partials       289      288       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This change prevents duplicate peers with the same address but different IDs, ensuring more accurate peer tracking.
- Moved responsability for keeping metadata to the Swarm type.
- Number of seeder and leechers is now calculated when the Swarm changes
  not on-demand. We avoid iterating over the peers to get the number of
seeders and leechers.
- The number of downloads is also calculate now in the Swarm. It will be
  removed from the TrackedTorrent.
@josecelano josecelano changed the title Overhaul stats: Improve torrent-repository package before adding events Overhaul stats: Improve torrent-repository package before adding events (part 1) May 6, 2025
josecelano added 2 commits May 6, 2025 18:48
Changed from:

```
/// Returns true if the torrents meets the retention policy, meaning that
/// it should be kept in the tracker.
pub fn meets_retaining_policy(&self, policy: &TrackerPolicy) -> bool {
    if policy.persistent_torrent_completed_stat && self.metadata().downloaded > 0 {
        return true;
    }

    if policy.remove_peerless_torrents && self.is_empty() {
        return false;
    }

    true
}
```

To:

```
pub fn meets_retaining_policy(&self, policy: &TrackerPolicy) -> bool {
    !(policy.remove_peerless_torrents && self.is_empty())
}
```

I think the first condition was introduced to avoid loosing the number of
downloads we¡hen the torrent is removed becuase there are no peers.

Now, we load that number from database when the torrent is added again
after removing it from the tracker.
@josecelano
Copy link
Member Author

ACK 728de22

@josecelano josecelano marked this pull request as ready for review May 6, 2025 17:56
@josecelano josecelano merged commit c68570c into torrust:develop May 6, 2025
21 checks passed
josecelano added a commit that referenced this pull request May 7, 2025
…e adding events (part 2)

4d91738 refactor: [#1495] renamings in torrent-repository pkg (Jose Celano)
5b3142f refactor: [#1495] refactor Swarms::upsert_peer (Jose Celano)
5c2c1e0 feat: [#1495] add len and is_empty methods to Swarms type (Jose Celano)
31f1fbf refactgor: [#1495] make field private (Jose Celano)
6d50fa0 refactor: [#1495] remove panics from Swarms type (Jose Celano)
5413e59 refactor: [#1495] renamings to follow latest changes in torrent-repository pkg (Jose Celano)
6f5cb27 refactor: [#1495] remove test for SwarmHandle (Jose Celano)

Pull request description:

  Improve `torrent-repository` package before adding events.

  This is a continuation of [tracker/issues/1495](#1497).

  #### Refactoring `TorrentRepository`

  - [x] Return errors instead of panicking in `Swarms` type.
  - [x] Make field `swarms` private.
  - [x] Add `len` and `is_empty` methods.
  - [x] Refactor `upsert_peer`.
  - [x] Renamings to follow the latest changes in type names.

ACKs for top commit:
  josecelano:
    ACK 4d91738

Tree-SHA512: 4ee9be1c21a62148d0d4fa0a9263c54312239fc98e2540d7583689a50126883ba563b5c676a606196c5696b14a4452a14061159b3ac2c69dd02c493c7930ceaf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Code Cleanup / Refactoring Tidying and Making Neat

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant