Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tracker error timeout #112

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cratetorrent/src/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub struct TorrentConf {
/// After this many attempts, the torrent stops announcing to a tracker.
pub tracker_error_threshold: usize,

/// Timeout duration after error threshold is reached.
pub tracker_error_timeout: Duration,

/// Specifies which optional alerts to send, besides the default periodic
/// stats update.
pub alerts: TorrentAlertConf,
Expand Down Expand Up @@ -96,10 +99,12 @@ impl Default for TorrentConf {
// This value is mostly picked for performance while keeping in mind
// not to overwhelm the host.
max_connected_peer_count: 50,
// needs teting
// TODO: needs testing
announce_interval: Duration::from_secs(60 * 60),
// needs testing
// TODO: needs testing
tracker_error_threshold: 15,
// TODO: needs testing
tracker_error_timeout: Duration::from_secs(60 * 60),
alerts: Default::default(),
}
}
Expand Down
27 changes: 21 additions & 6 deletions cratetorrent/src/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,13 +501,14 @@ impl Torrent {
let left = self.ctx.storage.download_len - downloaded;

// skip trackers that errored too often
// TODO: introduce a retry timeout
let tracker_error_threshold = self.conf.tracker_error_threshold;
for tracker in self
.trackers
.iter_mut()
.filter(|t| t.error_count < tracker_error_threshold)
{
let tracker_error_timeout = self.conf.tracker_error_timeout;
for tracker in self.trackers.iter_mut().filter(|t| {
match t.error_threshold_reached_time {
Some(instant) => instant.elapsed() > tracker_error_timeout,
None => true,
}
}) {
// Check if the torrent's peer count has fallen below the minimum.
// But don't request new peers otherwise or if we're about to stop
// torrent.
Expand Down Expand Up @@ -618,6 +619,14 @@ impl Torrent {
e
);
tracker.error_count += 1;
tracker.error_count_since_timeout += 1;
if tracker.error_count_since_timeout
> tracker_error_threshold
{
tracker.error_count_since_timeout = 0;
tracker.error_threshold_reached_time =
Some(Instant::now());
}
self.ctx.alert_tx.send(Alert::Error(
Error::Tracker {
id: self.ctx.id,
Expand Down Expand Up @@ -906,6 +915,10 @@ struct TrackerEntry {
/// Each time we fail to requet from tracker, this counter is incremented.
/// If it fails too often, we stop requesting from tracker.
error_count: usize,

error_count_since_timeout: usize,

error_threshold_reached_time: Option<Instant>,
}

impl TrackerEntry {
Expand All @@ -917,6 +930,8 @@ impl TrackerEntry {
interval: None,
min_interval: None,
error_count: 0,
error_count_since_timeout: 0,
error_threshold_reached_time: None,
}
}

Expand Down