This commit is contained in:
2022-06-16 07:38:20 +00:00
committed by GitHub
parent e2430d1d76
commit 61924c8160
4 changed files with 69 additions and 0 deletions

View File

@@ -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<T: Write + Seek + Send + Sync + ClearFile + GetTar
retry_count: AtomicI64,
/// The maximum retry count for each part.
pub max_part_retry_count: AtomicI64,
/// The maximun threads to download file.
pub max_threads: AtomicU64,
}
impl DownloaderInternal<LocalFile> {
@@ -163,6 +166,7 @@ impl DownloaderInternal<LocalFile> {
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<T: Write + Seek + Send + Sync + ClearFile + GetTargetFileName> 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<Duration>) {
@@ -545,6 +555,7 @@ impl<T: Write + Seek + Send + Sync + ClearFile + GetTargetFileName + 'static> Do
if helper.multiple_threads_download() {
self.enable_multiple_download()
}
self.set_max_threads(helper.max_threads());
}
#[inline]
@@ -561,6 +572,12 @@ impl<T: Write + Seek + Send + Sync + ClearFile + GetTargetFileName + 'static> 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<Duration>) {

View File

@@ -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 {

View File

@@ -80,6 +80,8 @@ pub struct CommandOpts {
pub multiple_threads_download: Option<bool>,
/// Max retry count of each part when downloading in multiple thread mode.
pub download_part_retry: Option<i64>,
/// The maximun threads when downloading file.
pub max_threads: Option<u64>,
}
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<T: AsRef<str>>(s: Option<T>) -> Result<Option<i64>, ParseIntErr
}
}
/// Prase [u64] from string
pub fn parse_u64<T: AsRef<str>>(s: Option<T>) -> Result<Option<u64>, ParseIntError> {
match s {
Some(s) => {
let s = s.as_ref();
let s = s.trim();
let c = s.parse::<u64>()?;
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<CommandOpts> {
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<CommandOpts> {
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
}

View File

@@ -25,6 +25,7 @@ pub fn get_settings_list() -> Vec<SettingDes> {
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