Skip to content

Commit 7256b46

Browse files
committed
feat: [#473] add timeout to Tracker API Client requests
Default timeout of 5 seconds.
1 parent 5a854f9 commit 7256b46

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/tracker/api.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use reqwest::{Error, Response};
24
pub struct ConnectionInfo {
35
/// The URL of the tracker. Eg: <https://tracker:7070> or <udp://tracker:6969>
@@ -15,6 +17,7 @@ impl ConnectionInfo {
1517

1618
pub struct Client {
1719
pub connection_info: ConnectionInfo,
20+
timeout: Duration,
1821
base_url: String,
1922
}
2023

@@ -24,6 +27,7 @@ impl Client {
2427
let base_url = format!("{}/api/v1", connection_info.url);
2528
Self {
2629
connection_info,
30+
timeout: Duration::from_secs(5),
2731
base_url,
2832
}
2933
}
@@ -36,7 +40,7 @@ impl Client {
3640
pub async fn whitelist_torrent(&self, info_hash: &str) -> Result<Response, Error> {
3741
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);
3842

39-
let client = reqwest::Client::new();
43+
let client = reqwest::Client::builder().timeout(self.timeout).build()?;
4044

4145
let params = [("token", &self.connection_info.token)];
4246

@@ -51,7 +55,7 @@ impl Client {
5155
pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Result<Response, Error> {
5256
let request_url = format!("{}/whitelist/{}", self.base_url, info_hash);
5357

54-
let client = reqwest::Client::new();
58+
let client = reqwest::Client::builder().timeout(self.timeout).build()?;
5559

5660
let params = [("token", &self.connection_info.token)];
5761

@@ -66,7 +70,7 @@ impl Client {
6670
pub async fn retrieve_new_tracker_key(&self, token_valid_seconds: u64) -> Result<Response, Error> {
6771
let request_url = format!("{}/key/{}", self.base_url, token_valid_seconds);
6872

69-
let client = reqwest::Client::new();
73+
let client = reqwest::Client::builder().timeout(self.timeout).build()?;
7074

7175
let params = [("token", &self.connection_info.token)];
7276

@@ -81,7 +85,7 @@ impl Client {
8185
pub async fn get_torrent_info(&self, info_hash: &str) -> Result<Response, Error> {
8286
let request_url = format!("{}/torrent/{}", self.base_url, info_hash);
8387

84-
let client = reqwest::Client::new();
88+
let client = reqwest::Client::builder().timeout(self.timeout).build()?;
8589

8690
let params = [("token", &self.connection_info.token)];
8791

0 commit comments

Comments
 (0)