-
Notifications
You must be signed in to change notification settings - Fork 48
Closed
Copy link
Labels
- Developer -Torrust Improvement ExperienceTorrust Improvement ExperienceCode Cleanup / RefactoringTidying and Making NeatTidying and Making Neat
Description
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:
PeerListTrackedTorrentTorrentRepository
Refactoring PeerList
- Rename
PeerListtoPeerAnnouncementMap?- 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_policybehavior. - Consider moving the
number_of_downloads_increasedcounter toPeerList. - Add unit tests.
Refactoring TorrentRepository
- Remove
LockTrackedTorrenttrait and return the error to upper layers. - Add a method for
len()oris_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 inupsert_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
Labels
- Developer -Torrust Improvement ExperienceTorrust Improvement ExperienceCode Cleanup / RefactoringTidying and Making NeatTidying and Making Neat