From 61924c8160bca5bd3cdf04e872102b1e97441fdd Mon Sep 17 00:00:00 2001 From: lifegpc Date: Thu, 16 Jun 2022 07:38:20 +0000 Subject: [PATCH] Update --- src/downloader/downloader.rs | 17 +++++++++++++++++ src/opthelper.rs | 15 +++++++++++++++ src/opts.rs | 31 +++++++++++++++++++++++++++++++ src/settings_list.rs | 6 ++++++ 4 files changed, 69 insertions(+) diff --git a/src/downloader/downloader.rs b/src/downloader/downloader.rs index 1a5ae4c..b8e9668 100644 --- a/src/downloader/downloader.rs +++ b/src/downloader/downloader.rs @@ -33,6 +33,7 @@ use std::ops::DerefMut; use std::path::Path; use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicI64; +use std::sync::atomic::AtomicU64; use std::sync::Arc; use std::sync::RwLock; use std::time::Duration; @@ -83,6 +84,8 @@ pub struct DownloaderInternal { @@ -163,6 +166,7 @@ impl DownloaderInternal { retry_interval: RwLock::new(l), retry_count: AtomicI64::new(0), max_part_retry_count: AtomicI64::new(3), + max_threads: AtomicU64::new(8), })) } } @@ -385,6 +389,12 @@ impl DownloaderIn self.max_retry_count.qstore(max_retry_count) } + #[inline] + /// Set the maximun threads to download file. + pub fn set_max_threads(&self, max_threads: u64) { + self.max_threads.qstore(max_threads) + } + #[inline] /// Set the retry interval. pub fn set_retry_interval(&self, retry_interval: NonTailList) { @@ -545,6 +555,7 @@ impl Do if helper.multiple_threads_download() { self.enable_multiple_download() } + self.set_max_threads(helper.max_threads()); } #[inline] @@ -561,6 +572,12 @@ impl Do self.downloader.set_max_retry_count(max_retry_count) } + #[inline] + /// Set the maximun threads to download file. + pub fn set_max_threads(&self, max_threads: u64) { + self.downloader.set_max_threads(max_threads) + } + #[inline] /// Set the retry interval. pub fn set_retry_interval(&self, retry_interval: NonTailList) { diff --git a/src/opthelper.rs b/src/opthelper.rs index 6ac1711..5d1f02c 100644 --- a/src/opthelper.rs +++ b/src/opthelper.rs @@ -102,6 +102,21 @@ impl OptHelper { } } + /// Return the maximun threads when downloading file. + pub fn max_threads(&self) -> u64 { + match self.opt.get_ref().max_threads { + Some(r) => { + return r; + } + None => {} + } + let re = self.settings.get_ref().get("max-threads"); + if re.is_some() { + return re.unwrap().as_u64().unwrap(); + } + 8 + } + /// Return whether to enable multiple threads download. pub fn multiple_threads_download(&self) -> bool { match self.opt.get_ref().multiple_threads_download { diff --git a/src/opts.rs b/src/opts.rs index 3e3e2ea..38fa18b 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -80,6 +80,8 @@ pub struct CommandOpts { pub multiple_threads_download: Option, /// Max retry count of each part when downloading in multiple thread mode. pub download_part_retry: Option, + /// The maximun threads when downloading file. + pub max_threads: Option, } impl CommandOpts { @@ -104,6 +106,7 @@ impl CommandOpts { download_retry_interval: None, multiple_threads_download: None, download_part_retry: None, + max_threads: None, } } @@ -186,6 +189,19 @@ pub fn parse_i64>(s: Option) -> Result, ParseIntErr } } +/// Prase [u64] from string +pub fn parse_u64>(s: Option) -> Result, ParseIntError> { + match s { + Some(s) => { + let s = s.as_ref(); + let s = s.trim(); + let c = s.parse::()?; + Ok(Some(c)) + } + None => Ok(None), + } +} + /// Parse optional option /// * `opts` - The result of options. See [getopts::Matches]. /// * `key` - The key of the option. @@ -306,6 +322,12 @@ pub fn parse_cmd() -> Option { gettext("Max retry count of each part when downloading in multiple thread mode."), "COUNT", ); + opts.optopt( + "m", + "max-threads", + gettext("The maximun threads when downloading file."), + "COUNT", + ); let result = match opts.parse(&argv[1..]) { Ok(m) => m, Err(err) => { @@ -495,6 +517,15 @@ pub fn parse_cmd() -> Option { return None; } } + match parse_u64(result.opt_str("max-threads")) { + Ok(r) => { + re.as_mut().unwrap().max_threads = r; + } + Err(e) => { + println!("{} {}", gettext("Failed to parse max threads:"), e); + return None; + } + } re } diff --git a/src/settings_list.rs b/src/settings_list.rs index 296dedf..da4d926 100644 --- a/src/settings_list.rs +++ b/src/settings_list.rs @@ -25,6 +25,7 @@ pub fn get_settings_list() -> Vec { SettingDes::new("download-retry-interval", gettext("The interval (in seconds) between two retries when downloading files."), JsonValueType::Multiple, Some(check_retry_interval)).unwrap(), SettingDes::new("multiple-threads-download", gettext("Whether to enable multiple threads download."), JsonValueType::Boolean, None).unwrap(), SettingDes::new("download-part-retry", gettext("Max retry count of each part when downloading in multiple thread mode."), JsonValueType::Number, Some(check_i64)).unwrap(), + SettingDes::new("max-threads", gettext("The maximun threads when downloading file."), JsonValueType::Number, Some(check_u64)).unwrap(), ] } @@ -33,6 +34,11 @@ fn check_i64(obj: &JsonValue) -> bool { r.is_some() } +fn check_u64(obj: &JsonValue) -> bool { + let r = obj.as_u64(); + r.is_some() +} + fn check_nonempty_str(obj: &JsonValue) -> bool { let r = obj.as_str(); r.is_some() && r.unwrap().len() != 0