Skip to content

Commit 0a0b59e

Browse files
authored
Merge pull request #135 from wallabra/feat/cfg-parallel-download
feat(config): Add global configuration option to set a limit on parallel downloads
2 parents 80239cb + f7628f1 commit 0a0b59e

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

crates/download-manager/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,18 @@ pub enum DownloadManagerMessage {
2222
pub struct DownloadManager {
2323
database: &'static YTLocalDatabase,
2424
cache_dir: PathBuf,
25+
parallel_downloads: u16,
2526
handles: Mutex<Vec<JoinHandle<()>>>,
2627
download_list: Mutex<VecDeque<YoutubeMusicVideoRef>>,
2728
in_download: Mutex<HashSet<String>>,
2829
}
2930

3031
impl DownloadManager {
31-
pub fn new(cache_dir: PathBuf, database: &'static YTLocalDatabase) -> Self {
32+
pub fn new(cache_dir: PathBuf, database: &'static YTLocalDatabase, parallel_downloads: u16) -> Self {
3233
Self {
3334
database,
3435
cache_dir,
36+
parallel_downloads,
3537
handles: Mutex::new(Vec::new()),
3638
download_list: Mutex::new(VecDeque::new()),
3739
in_download: Mutex::new(HashSet::new()),
@@ -78,7 +80,7 @@ impl DownloadManager {
7880
cancelation: impl Future<Output = ()> + Clone + Send + 'static,
7981
sender: MessageHandler,
8082
) {
81-
for _ in 0..DOWNLOADER_COUNT {
83+
for _ in 0..self.parallel_downloads {
8284
self.run_service_stream(cancelation.clone(), sender.clone());
8385
}
8486
}
@@ -111,5 +113,3 @@ impl DownloadManager {
111113
list.extend(to_add);
112114
}
113115
}
114-
115-
const DOWNLOADER_COUNT: usize = 4;

crates/ytermusic/src/config.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@ use crate::utils::get_project_dirs;
66

77
#[derive(Debug, Default, Deserialize, Serialize)]
88
#[non_exhaustive]
9-
pub struct GlobalConfig {}
9+
pub struct GlobalConfig {
10+
/// Maximum number of parallel downloads.
11+
/// If your downloads are failing, try lowering
12+
/// this.
13+
/// Default value is 4.
14+
#[serde(default = "parallel_downloads")]
15+
pub parallel_downloads: u16,
16+
}
1017

1118
#[derive(Debug, Deserialize, Serialize)]
1219
#[non_exhaustive]
@@ -94,6 +101,10 @@ fn default_error_style() -> Style {
94101
Style::default().fg(Color::Red)
95102
}
96103

104+
fn parallel_downloads() -> u16 {
105+
4
106+
}
107+
97108
fn default_false() -> bool {
98109
false
99110
}
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
use download_manager::DownloadManager;
22
use once_cell::sync::Lazy;
33

4-
use crate::{consts::CACHE_DIR, DATABASE};
4+
use crate::{
5+
consts::{CACHE_DIR, CONFIG},
6+
DATABASE,
7+
};
58

69
pub mod logger;
710
pub mod player;
811

9-
pub static DOWNLOAD_MANAGER: Lazy<DownloadManager> =
10-
Lazy::new(|| DownloadManager::new(CACHE_DIR.to_path_buf(), &DATABASE));
12+
pub static DOWNLOAD_MANAGER: Lazy<DownloadManager> = Lazy::new(|| {
13+
DownloadManager::new(
14+
CACHE_DIR.to_path_buf(),
15+
&DATABASE,
16+
CONFIG.global.parallel_downloads,
17+
)
18+
});

0 commit comments

Comments
 (0)