From 7256b4645b3b7fb92661a3b715742a97ad4b7132 Mon Sep 17 00:00:00 2001 From: Jose Celano Date: Mon, 12 Feb 2024 11:13:04 +0000 Subject: [PATCH] feat: [#473] add timeout to Tracker API Client requests Default timeout of 5 seconds. --- src/tracker/api.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/tracker/api.rs b/src/tracker/api.rs index f64430b7..39a7b42b 100644 --- a/src/tracker/api.rs +++ b/src/tracker/api.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use reqwest::{Error, Response}; pub struct ConnectionInfo { /// The URL of the tracker. Eg: or @@ -15,6 +17,7 @@ impl ConnectionInfo { pub struct Client { pub connection_info: ConnectionInfo, + timeout: Duration, base_url: String, } @@ -24,6 +27,7 @@ impl Client { let base_url = format!("{}/api/v1", connection_info.url); Self { connection_info, + timeout: Duration::from_secs(5), base_url, } } @@ -36,7 +40,7 @@ impl Client { pub async fn whitelist_torrent(&self, info_hash: &str) -> Result { let request_url = format!("{}/whitelist/{}", self.base_url, info_hash); - let client = reqwest::Client::new(); + let client = reqwest::Client::builder().timeout(self.timeout).build()?; let params = [("token", &self.connection_info.token)]; @@ -51,7 +55,7 @@ impl Client { pub async fn remove_torrent_from_whitelist(&self, info_hash: &str) -> Result { let request_url = format!("{}/whitelist/{}", self.base_url, info_hash); - let client = reqwest::Client::new(); + let client = reqwest::Client::builder().timeout(self.timeout).build()?; let params = [("token", &self.connection_info.token)]; @@ -66,7 +70,7 @@ impl Client { pub async fn retrieve_new_tracker_key(&self, token_valid_seconds: u64) -> Result { let request_url = format!("{}/key/{}", self.base_url, token_valid_seconds); - let client = reqwest::Client::new(); + let client = reqwest::Client::builder().timeout(self.timeout).build()?; let params = [("token", &self.connection_info.token)]; @@ -81,7 +85,7 @@ impl Client { pub async fn get_torrent_info(&self, info_hash: &str) -> Result { let request_url = format!("{}/torrent/{}", self.base_url, info_hash); - let client = reqwest::Client::new(); + let client = reqwest::Client::builder().timeout(self.timeout).build()?; let params = [("token", &self.connection_info.token)];