Skip to content

Commit

Permalink
request retry
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaan26 committed Jan 10, 2025
1 parent 80d5ea8 commit 49871d2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt()
// all spans/events with a level higher than TRACE (e.g, info, warn, etc.)
// will be written to stdout.
.with_env_filter("zung=trace")
.with_env_filter("zung=info")
.without_time()
.compact()
// display source code file paths
Expand Down
4 changes: 1 addition & 3 deletions zung_torrent/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ impl TorrentArgs {

if let Some(list) = torrent.sources().tracker_list() {
for t in list {
if t.is_connected() {
println!("{} -> {}", t.url().cyan(), t.trys())
}
println!("{} -> {}", t.trys(), t.url().cyan());
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions zung_torrent/src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ impl DownloadSources {
}

Check warning on line 217 in zung_torrent/src/sources/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/mod.rs#L214-L217

Added lines #L214 - L217 were not covered by tests

pub async fn retry_connect_all(&self, info_hash: InfoHashEncoded, peer_id: PeerID) {
println!("hi");
if let Some(list) = self.tracker_list() {
let mut handles = Vec::new();
for tracker in list {
if !tracker.is_connected() {
let tracker = tracker.clone();
tokio::spawn(async move {
let handle = tokio::spawn(async move {
let mut i = 0;

Check warning on line 226 in zung_torrent/src/sources/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/mod.rs#L219-L226

Added lines #L219 - L226 were not covered by tests
loop {
i += 1;
Expand All @@ -238,13 +238,23 @@ impl DownloadSources {
Err(_) => {
if i < 10 {
continue;

Check warning on line 240 in zung_torrent/src/sources/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/mod.rs#L239-L240

Added lines #L239 - L240 were not covered by tests
} else {
break;

Check warning on line 242 in zung_torrent/src/sources/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/mod.rs#L242

Added line #L242 was not covered by tests
}
}
}
}
});

handles.push(handle);
}

Check warning on line 250 in zung_torrent/src/sources/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/mod.rs#L247-L250

Added lines #L247 - L250 were not covered by tests
}

tokio::spawn(async {
futures::future::join_all(handles).await;
})
.await
.unwrap();

Check warning on line 257 in zung_torrent/src/sources/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/mod.rs#L253-L257

Added lines #L253 - L257 were not covered by tests
}
}

Expand Down
6 changes: 2 additions & 4 deletions zung_torrent/src/sources/trackers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ impl Tracker {
info_hash: InfoHashEncoded,
peer_id: PeerID,
) -> Result<()> {
// Generate Tracker request.
// - Http => Generates a url.
// - UDP => Sends a UDP connect request
self.inner.trys.fetch_add(1, Ordering::SeqCst);

Check warning on line 73 in zung_torrent/src/sources/trackers/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/trackers/mod.rs#L67-L73

Added lines #L67 - L73 were not covered by tests

let request = self
.url
.generate_request(socket, info_hash, peer_id)
Expand All @@ -82,7 +81,6 @@ impl Tracker {
let response = request.make_request().await?;

Check warning on line 81 in zung_torrent/src/sources/trackers/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/trackers/mod.rs#L81

Added line #L81 was not covered by tests

self.inner.connected.store(true, Ordering::Relaxed);
self.inner.trys.fetch_add(1, Ordering::SeqCst);

self.set_request(request)?;
self.set_response(response)?;

Check warning on line 86 in zung_torrent/src/sources/trackers/mod.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/trackers/mod.rs#L83-L86

Added lines #L83 - L86 were not covered by tests
Expand Down
7 changes: 4 additions & 3 deletions zung_torrent/src/sources/trackers/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::Serialize;

use tokio::net::UdpSocket;
use tokio::time::timeout;
use tracing::{instrument, trace, warn};
use tracing::{instrument, trace};
use zung_parsers::bencode;

use crate::meta_info::InfoHashEncoded;
Expand Down Expand Up @@ -88,9 +88,10 @@ impl TrackerRequest {

#[instrument(skip_all, name = "Tracker Request")]
pub async fn make_request(&self) -> Result<bencode::Value> {
let url = self.to_url()?;

match self {
TrackerRequest::Http { .. } => {
let url = self.to_url()?;
let request = timeout(REQUEST_TIMEOUT_DURATION, reqwest::get(&url))
.await
.with_context(|| format!("Connection Timed Out: {url}"))?

Check warning on line 97 in zung_torrent/src/sources/trackers/request.rs

View check run for this annotation

Codecov / codecov/patch

zung_torrent/src/sources/trackers/request.rs#L97

Added line #L97 was not covered by tests
Expand All @@ -106,7 +107,7 @@ impl TrackerRequest {
Ok(response)
}
TrackerRequest::Udp { .. } => {
warn!(url = self.to_url()?, "UDP connection is to be implemented");
// warn!(url = self.to_url()?, "UDP connection is to be implemented");
Err(anyhow!(""))
}
TrackerRequest::Empty => Err(anyhow!("Making Request on empty string")),
Expand Down

0 comments on commit 49871d2

Please sign in to comment.