Skip to content

Commit

Permalink
Integrate trackers into torrent event loop
Browse files Browse the repository at this point in the history
  • Loading branch information
vimpunk committed Nov 29, 2020
1 parent 71faf3d commit edf250d
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 31 deletions.
3 changes: 1 addition & 2 deletions DESIGN.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ Each tracker HTTP request runs asynchronously and is polled by the torrent
event loop's select call.

Periodically each torrent also sends progress updates to the tracker. The
periodicity is kept low here to not take up bandwidth. It will be configurable
in the future.
periodicity is defined by the tracker, but it may be configurable in the future.

### Peer sessions

Expand Down
10 changes: 9 additions & 1 deletion cratetorrent/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
metainfo::Metainfo,
storage_info::StorageInfo,
torrent::Torrent,
tracker::Tracker,
Bitfield, PeerId,
};

Expand Down Expand Up @@ -115,6 +116,11 @@ async fn start_engine(
return Ok(());
};

let mut trackers = Vec::with_capacity(metainfo.trackers.len());
for tracker_url in metainfo.trackers.into_iter() {
trackers.push(Tracker::new(tracker_url));
}

let own_pieces = mode.own_pieces(storage_info.piece_count);
let mut torrent = Torrent::new(
id,
Expand All @@ -123,10 +129,12 @@ async fn start_engine(
info_hash,
storage_info,
own_pieces,
trackers,
client_id,
listen_addr,
);
let seeds = mode.seeds();
torrent.start(listen_addr, &seeds).await?;
torrent.start(&seeds).await?;

// send a shutdown command to disk
disk.shutdown()?;
Expand Down
2 changes: 1 addition & 1 deletion cratetorrent/src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl PeerSession {
// keep-alive

// update status
self.state.tick();
self.state.tick(&self.torrent.stats);

peer_info!(
self,
Expand Down
30 changes: 26 additions & 4 deletions cratetorrent/src/peer/state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::time::{Duration, Instant};
use std::{
sync::atomic::Ordering,
time::{Duration, Instant},
};

use crate::{
avg::SlidingDurationAvg, counter::Counter, Bitfield, PeerId, BLOCK_LEN,
avg::SlidingDurationAvg, counter::Counter, torrent::TorrentStats, Bitfield,
PeerId, BLOCK_LEN,
};

/// Holds and provides facilities to modify the state of a peer session.
Expand Down Expand Up @@ -183,7 +187,7 @@ impl SessionState {
/// Updates various statistics and session state.
///
/// This should be called every second.
pub fn tick(&mut self) {
pub fn tick(&mut self, torrent_stats: &TorrentStats) {
self.maybe_exit_slow_start();

// NOTE: This has to be *after* `maybe_exit_slow_start` and *before*
Expand All @@ -199,9 +203,27 @@ impl SessionState {
if !self.request_timed_out {
self.update_target_request_queue_len();
}

// copy over this round's transfer tally to the torrent's stats
torrent_stats.downloaded_protocol_count.fetch_add(
self.downloaded_protocol_counter.total(),
Ordering::Relaxed,
);
torrent_stats.uploaded_protocol_count.fetch_add(
self.uploaded_protocol_counter.total(),
Ordering::Relaxed,
);
torrent_stats.downloaded_payload_count.fetch_add(
self.downloaded_payload_counter.total(),
Ordering::Relaxed,
);
torrent_stats.uploaded_payload_count.fetch_add(
self.uploaded_payload_counter.total(),
Ordering::Relaxed,
);
}

/// Check if we need to exit slow start.
/// Checks if we need to exit slow start.
///
/// We leave slow start if the download rate has not increased
/// significantly since the last round.
Expand Down
Loading

0 comments on commit edf250d

Please sign in to comment.