From 912bc9d915b8cf4e5e1d39cd48bed3fef7ce2b2f Mon Sep 17 00:00:00 2001 From: Kenneth Gitere Date: Sun, 21 Feb 2021 12:40:17 +0300 Subject: [PATCH] Add flag for configuring maximum concurrent requests Change printing macro for error messages to go out to stderr --- src/cli.rs | 20 ++++++++++++++++---- src/main.rs | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 33b5072..3f0f9d9 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -33,7 +33,12 @@ It takes a url and downloads the article content from it and saves it to an epub .help("Merge multiple articles into a single epub") .long_help("Merge multiple articles into a single epub that will be given the name provided") .takes_value(true), - ); + ).arg( + Arg::with_name("max_conn") + .long("max_conn") + .help("The maximum number of concurrent HTTP connections when downloading articles. Default is 8") + .long_help("The maximum number of concurrent HTTP connections when downloading articles. Default is 8.\nNOTE: It is advised to use as few connections as needed i.e between 1 and 50. Using more connections can end up overloading your network card with too many concurrent requests.") + .takes_value(true)); let arg_matches = app.get_matches(); let mut urls: Vec = match arg_matches.value_of("file") { Some(file_name) => { @@ -62,7 +67,14 @@ It takes a url and downloads the article content from it and saves it to an epub ); } - let mut app_config = AppConfig::new(); + let max_conn = arg_matches + .value_of("max_conn") + .map(|conn_str| conn_str.parse::().ok()) + .flatten() + .map(|max| if max > 0 { max } else { 1 }) + .unwrap_or(8); + + let mut app_config = AppConfig::new(max_conn); app_config.set_urls(urls); if let Some(name) = arg_matches.value_of("output_name") { let file_name = if name.ends_with(".epub") && name.len() > 5 { @@ -82,10 +94,10 @@ pub struct AppConfig { } impl AppConfig { - fn new() -> Self { + fn new(max_conn: usize) -> Self { Self { urls: vec![], - max_conn: 8, + max_conn, merged: None, } } diff --git a/src/main.rs b/src/main.rs index 5ba4ad9..0467712 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,7 @@ fn download(app_config: AppConfig) { articles.push(extractor); } } - Err(e) => println!("{}", e), + Err(e) => eprintln!("{}", e), } } articles