Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mickvandijke committed Feb 1, 2024
1 parent 65532d1 commit 04e4f8f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/core/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,42 @@ mod tests {
]
);
}

#[tokio::test]
async fn should_return_all_torrent_info_hashes() {
let tracker = Arc::new(tracker_factory(&tracker_configuration()));

let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned();
let info_hash1 = InfoHash::from_str(&hash1).unwrap();
tracker
.update_torrent_with_peer_and_get_stats(&info_hash1, &sample_peer())
.await;

let hash2 = "03840548643af2a7b63a9f5cbca348bc7150ca3a".to_owned();
let info_hash2 = InfoHash::from_str(&hash2).unwrap();
tracker
.update_torrent_with_peer_and_get_stats(&info_hash2, &sample_peer())
.await;

let torrents = get_torrents(tracker.clone(), &Pagination::default()).await;

assert_eq!(
torrents,
vec![
BasicInfo {
info_hash: InfoHash::from_str(&hash2).unwrap(),
seeders: 1,
completed: 0,
leechers: 0,
},
BasicInfo {
info_hash: InfoHash::from_str(&hash1).unwrap(),
seeders: 1,
completed: 0,
leechers: 0,
}
]
);
}
}
}
12 changes: 11 additions & 1 deletion src/core/torrent/repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,29 +389,37 @@ impl Repository for RepositoryDashmap {
fn upsert_torrent_with_peer_and_get_stats(&self, info_hash: &InfoHash, peer: &peer::Peer) -> (SwarmStats, bool) {
let hash = self.torrents.hash_usize(&info_hash);
let shard_idx = self.torrents.determine_shard(hash);

// Is safe as it tries to get an array item at a certain index that we know exists for sure
let mut shard = unsafe { self.torrents._yield_write_shard(shard_idx) };

// Remove the torrent from the shard
let mut torrent = shard.remove(info_hash).map(|v| v.into_inner()).unwrap_or_default();

let stats_updated = torrent.insert_or_update_peer(peer);
let stats = torrent.get_stats();

let mut mem_size_shard: usize = 0;

// Calculate and set the current mem size of the shard
for torrent in shard.values() {
mem_size_shard += (2 * POINTER_SIZE) + INFO_HASH_SIZE + torrent.get().get_mem_size();
}

// Get the max memory limit per shard
let maybe_max_memory_available = MAX_MEMORY_LIMIT.map(|v| v / self.torrents._shard_count() - mem_size_shard);

// Calculate the shortage of memory on the shard
let memory_shortage = maybe_max_memory_available
.map(|v| TORRENT_INSERTION_SIZE_COST.saturating_sub(v))
.unwrap_or(0);

// Free the needed memory on the shard if there is a shortage
if memory_shortage > 0 {
let mut amount_freed: usize = 0;

let mut priority_list = unsafe { self.shard_priority_list.get_unchecked(shard_idx) }.lock().unwrap();
// Unwrap is safe as we try to get an array item at a certain index that we know exists for sure
let mut priority_list = self.shard_priority_list.get(shard_idx).unwrap().lock().unwrap();

while amount_freed < memory_shortage && !priority_list.is_empty() {
// Can safely unwrap as we check if the priority list is not empty
Expand All @@ -423,8 +431,10 @@ impl Repository for RepositoryDashmap {
}
}

// Add or shift the torrent info hash to the top of the shard priority list
self.addshift_torrent_to_front_on_shard_priority_list(shard_idx, info_hash);

// (re)insert (updated) torrent into the shard
shard
.insert(info_hash.to_owned(), SharedValue::new(torrent))
.map(|v| v.into_inner());
Expand Down

0 comments on commit 04e4f8f

Please sign in to comment.