Browse Source

Set explicit client timeouts, fix timeout for big transfers

timvisee 6 years ago
parent
commit
a6fdde6d0f
10 changed files with 54 additions and 16 deletions
  1. 3 2
      src/action/delete.rs
  2. 3 3
      src/action/download.rs
  3. 2 2
      src/action/exists.rs
  4. 2 2
      src/action/info.rs
  5. 2 2
      src/action/params.rs
  6. 2 2
      src/action/password.rs
  7. 3 3
      src/action/upload.rs
  8. 27 0
      src/client.rs
  9. 8 0
      src/config.rs
  10. 2 0
      src/main.rs

+ 3 - 2
src/action/delete.rs

@@ -1,8 +1,8 @@
 use clap::ArgMatches;
 use ffsend_api::action::delete::{Delete as ApiDelete, Error as DeleteError};
 use ffsend_api::file::remote_file::{FileParseError, RemoteFile};
-use ffsend_api::reqwest::Client;
 
+use client::create_client;
 use cmd::matcher::{delete::DeleteMatcher, main::MainMatcher, Matcher};
 use error::ActionError;
 #[cfg(feature = "history")]
@@ -31,7 +31,8 @@ impl<'a> Delete<'a> {
         let url = matcher_delete.url();
 
         // Create a reqwest client
-        let client = Client::new();
+        // TODO: create transfer client when draining downloads
+        let client = create_client();
 
         // Parse the remote file based on the share link, derive the owner token from history
         let mut file = RemoteFile::parse_url(url, matcher_delete.owner())?;

+ 3 - 3
src/action/download.rs

@@ -12,12 +12,12 @@ use ffsend_api::action::exists::{Error as ExistsError, Exists as ApiExists};
 use ffsend_api::action::metadata::{Error as MetadataError, Metadata as ApiMetadata};
 use ffsend_api::file::remote_file::{FileParseError, RemoteFile};
 use ffsend_api::reader::ProgressReporter;
-use ffsend_api::reqwest::Client;
 #[cfg(feature = "archive")]
 use tempfile::{Builder as TempBuilder, NamedTempFile};
 
 #[cfg(feature = "archive")]
 use archive::archive::Archive;
+use client::create_transfer_client;
 use cmd::matcher::{download::DownloadMatcher, main::MainMatcher, Matcher};
 #[cfg(feature = "history")]
 use history_tool;
@@ -47,8 +47,8 @@ impl<'a> Download<'a> {
         // Get the share URL
         let url = matcher_download.url();
 
-        // Create a reqwest client
-        let client = Client::new();
+        // Create a reqwest client capable for downloading files
+        let client = create_transfer_client();
 
         // Parse the remote file based on the share URL
         let file = RemoteFile::parse_url(url, None)?;

+ 2 - 2
src/action/exists.rs

@@ -1,8 +1,8 @@
 use clap::ArgMatches;
 use ffsend_api::action::exists::{Error as ExistsError, Exists as ApiExists};
 use ffsend_api::file::remote_file::{FileParseError, RemoteFile};
-use ffsend_api::reqwest::Client;
 
+use client::create_client;
 #[cfg(feature = "history")]
 use cmd::matcher::main::MainMatcher;
 use cmd::matcher::{exists::ExistsMatcher, Matcher};
@@ -33,7 +33,7 @@ impl<'a> Exists<'a> {
         let url = matcher_exists.url();
 
         // Create a reqwest client
-        let client = Client::new();
+        let client = create_client();
 
         // Parse the remote file based on the share URL
         let file = RemoteFile::parse_url(url, None)?;

+ 2 - 2
src/action/info.rs

@@ -5,9 +5,9 @@ use ffsend_api::action::exists::{Error as ExistsError, Exists as ApiExists};
 use ffsend_api::action::info::{Error as InfoError, Info as ApiInfo};
 use ffsend_api::action::metadata::Metadata as ApiMetadata;
 use ffsend_api::file::remote_file::{FileParseError, RemoteFile};
-use ffsend_api::reqwest::Client;
 use prettytable::{Cell, format::FormatBuilder, Row, Table};
 
+use client::create_client;
 use cmd::matcher::{info::InfoMatcher, main::MainMatcher, Matcher};
 #[cfg(feature = "history")]
 use history_tool;
@@ -35,7 +35,7 @@ impl<'a> Info<'a> {
         let url = matcher_info.url();
 
         // Create a reqwest client
-        let client = Client::new();
+        let client = create_client();
 
         // Parse the remote file based on the share URL, derive the owner token from history
         let mut file = RemoteFile::parse_url(url, matcher_info.owner())?;

+ 2 - 2
src/action/params.rs

@@ -1,8 +1,8 @@
 use clap::ArgMatches;
 use ffsend_api::action::params::{Error as ParamsError, Params as ApiParams, ParamsDataBuilder};
 use ffsend_api::file::remote_file::RemoteFile;
-use ffsend_api::reqwest::Client;
 
+use client::create_client;
 use cmd::matcher::{main::MainMatcher, params::ParamsMatcher, Matcher};
 use error::ActionError;
 #[cfg(feature = "history")]
@@ -31,7 +31,7 @@ impl<'a> Params<'a> {
         let url = matcher_params.url();
 
         // Create a reqwest client
-        let client = Client::new();
+        let client = create_client();
 
         // Parse the remote file based on the share URL, derive the owner token from history
         let mut file = RemoteFile::parse_url(url, matcher_params.owner())?;

+ 2 - 2
src/action/password.rs

@@ -1,9 +1,9 @@
 use clap::ArgMatches;
 use ffsend_api::action::password::{Error as PasswordError, Password as ApiPassword};
 use ffsend_api::file::remote_file::RemoteFile;
-use ffsend_api::reqwest::Client;
 use prettytable::{Cell, format::FormatBuilder, Row, Table};
 
+use client::create_client;
 use cmd::matcher::{main::MainMatcher, password::PasswordMatcher, Matcher};
 use error::ActionError;
 #[cfg(feature = "history")]
@@ -32,7 +32,7 @@ impl<'a> Password<'a> {
         let url = matcher_password.url();
 
         // Create a reqwest client
-        let client = Client::new();
+        let client = create_client();
 
         // Parse the remote file based on the share URL, derive the owner token from history
         let mut file = RemoteFile::parse_url(url, matcher_password.owner())?;

+ 3 - 3
src/action/upload.rs

@@ -10,13 +10,13 @@ use ffsend_api::action::params::ParamsDataBuilder;
 use ffsend_api::action::upload::{Error as UploadError, Upload as ApiUpload};
 use ffsend_api::config::{UPLOAD_SIZE_MAX, UPLOAD_SIZE_MAX_RECOMMENDED};
 use ffsend_api::reader::ProgressReporter;
-use ffsend_api::reqwest::Client;
 use prettytable::{Cell, format::FormatBuilder, Row, Table};
 #[cfg(feature = "archive")]
 use tempfile::{Builder as TempBuilder, NamedTempFile};
 
 #[cfg(feature = "archive")]
 use archive::archiver::Archiver;
+use client::create_transfer_client;
 use cmd::matcher::{MainMatcher, Matcher, UploadMatcher};
 #[cfg(feature = "history")]
 use history_tool;
@@ -90,8 +90,8 @@ impl<'a> Upload<'a> {
             print_error_msg("failed to check the file size, ignoring");
         }
 
-        // Create a reqwest client
-        let client = Client::new();
+        // Create a reqwest client capable for uploading files
+        let client = create_transfer_client();
 
         // Create a progress bar reporter
         let progress_bar = Arc::new(Mutex::new(ProgressBar::new_upload()));

+ 27 - 0
src/client.rs

@@ -0,0 +1,27 @@
+use std::time::Duration;
+
+use ffsend_api::reqwest::{Client, ClientBuilder};
+
+use config::{CLIENT_TIMEOUT, CLIENT_TRANSFER_TIMEOUT};
+
+/// Create the default client, which is used for generic Send requests.
+///
+/// Note: use `create_transfer_client()` instead for clients that upload/download.
+pub fn create_client() -> Client {
+    create_custom_client(CLIENT_TIMEOUT)
+}
+
+/// Create the default client, which is used for generic Send requests.
+///
+/// Note: use `create_transfer_client()` instead for clients that upload/download.
+pub fn create_transfer_client() -> Client {
+    create_custom_client(CLIENT_TRANSFER_TIMEOUT)
+}
+
+/// Create the Send client with a custom timeout.
+fn create_custom_client(timeout: Option<Duration>) -> Client {
+    ClientBuilder::new()
+        .timeout(timeout)
+        .build()
+        .expect("failed to build custom reqwest client")
+}

+ 8 - 0
src/config.rs

@@ -0,0 +1,8 @@
+use std::time::Duration;
+
+/// The timeout for the Send client for generic requests, `None` to disable.
+pub const CLIENT_TIMEOUT: Option<Duration> = Some(Duration::from_secs(30));
+
+/// The timeout for the Send client used to transfer (upload/download) files.
+/// Make sure this is big enough, or file uploads will be dropped. `None` to disable.
+pub const CLIENT_TRANSFER_TIMEOUT: Option<Duration> = Some(Duration::from_secs(24 * 60 * 60));

+ 2 - 0
src/main.rs

@@ -22,7 +22,9 @@ extern crate tempfile;
 mod action;
 #[cfg(feature = "archive")]
 mod archive;
+mod client;
 mod cmd;
+mod config;
 mod error;
 #[cfg(feature = "history")]
 mod history;