Skip to content

Overhaul stats: Improve torrent-repository package before adding events #1495

@josecelano

Description

@josecelano

I've been working on simplifying the torrent-repository package. While working on that, I have identified some things that can be improved or fixed.

I will do it before adding the events. Otherwise, it would be a bigger effort to do it later.

I will group the subtasks into three groups, one for each of the main types in the package:

  • PeerList
  • TrackedTorrent
  • TorrentRepository

Refactoring PeerList

  • Rename PeerList to PeerAnnouncementMap?
    • The Peer is the latest peer announcement, not only the IP.
    • It's not implemented with a list, although I chose list as a generic list, not because it's implemented with a "list"
  • Use the peer socket address instead of peer ID to avoid peers with duplicate socket addresses.
  • Use a cache for seeders and leechers counters to avoid iterating the list.
  • Increase test coverage.

Refactoring TrackedTorrent

  • Document meets_retaining_policy behavior.
  • Consider moving the number_of_downloads_increased counter to PeerList.
  • Add unit tests.

Refactoring TorrentRepository

  • Remove LockTrackedTorrent trait and return the error to upper layers.
  • Add a method for len() or is_empty()
  • Fix Concurrency edge case in upsert_peer

Replace:

if let Some(existing_entry) = self.torrents.get(info_hash) {
    // ...
} else {
    let inserted_entry = self.torrents.get_or_insert(...)
    // ...
}

With:

let inserted_entry = self.torrents.get_or_insert_with(*info_hash, || {
    TrackedTorrentHandle::new(TrackedTorrent { ... }.into())
});
let mut torrent_guard = inserted_entry.value().lock_or_panic();
torrent_guard.upsert_peer(peer)
  • Optimization: Early get() return in upsert_peer

Replace:

if let Some(existing_entry) = self.torrents.get(info_hash) {
    // mutate entry
} else {
    // insert and mutate
}

With:

let entry = self.torrents.get_or_insert_with(*info_hash, || {
    TrackedTorrentHandle::new(
        TrackedTorrent {
            swarm: PeerList::default(),
            downloaded: opt_persistent_torrent.unwrap_or_default(),
        }
        .into(),
    )
});

let mut guard = entry.value().lock_or_panic();
guard.upsert_peer(peer)

cc @da2ce7

Metadata

Metadata

Assignees

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions