Skip to content

Commit

Permalink
Limit parallel network requests to a default of 10, and allow confi…
Browse files Browse the repository at this point in the history
…guring it via a command line flag
  • Loading branch information
theRookieCoder committed Oct 2, 2024
1 parent 48531ac commit eb3cd33
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
21 changes: 11 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ tokio = { version = "1.40", default-features = false, features = [
"rt-multi-thread",
"macros",
] }
rustls = { version = "0.23", features = ["ring"] }
clap = { version = "4.5", features = ["derive"] }
clap_complete = "4.5"
serde_json = "1.0"
Expand Down
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Ferium {
/// You can also use the environment variable `TOKIO_WORKER_THREADS`.
#[clap(long, short)]
pub threads: Option<usize>,
/// Specify the maximum number of parallel network requests to perform.
#[clap(long, short = 'p')]
pub parallel_network: Option<usize>,
/// Set a GitHub personal access token for increasing the GitHub API rate limit.
/// You can also use the environment variable `GITHUB_TOKEN`.
#[clap(long, visible_alias = "gh")]
Expand Down
9 changes: 5 additions & 4 deletions src/download.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![expect(clippy::expect_used, reason = "For mutex poisons")]

use crate::{STYLE_BYTE, TICK};
use crate::{DEFAULT_PARALLEL_NETWORK, PARALLEL_NETWORK, STYLE_BYTE, TICK};
use anyhow::{anyhow, bail, Error, Result};
use colored::Colorize as _;
use fs_extra::{
Expand All @@ -19,8 +19,6 @@ use std::{
};
use tokio::sync::Semaphore;

const CONCURRENT_DOWNLOADS: usize = 6;

/// Check the given `directory`
///
/// - If there are files there that are not in `to_download` or `to_install`, they will be moved to `directory`/.old
Expand Down Expand Up @@ -115,7 +113,9 @@ pub async fn download(
.expect("Mutex poisoned")
.enable_steady_tick(Duration::from_millis(100));
let mut tasks = FuturesUnordered::new();
let semaphore = Arc::new(Semaphore::new(CONCURRENT_DOWNLOADS));
let semaphore = Arc::new(Semaphore::new(
*PARALLEL_NETWORK.get_or_init(|| DEFAULT_PARALLEL_NETWORK),
));
let client = reqwest::Client::new();

for downloadable in to_download {
Expand All @@ -126,6 +126,7 @@ pub async fn download(

tasks.push(async move {
let _permit = semaphore.acquire_owned().await?;

let (length, filename) = downloadable
.download(&client, &output_dir, |additional| {
progress_bar
Expand Down
10 changes: 9 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,15 @@ use libium::{
use std::{
env::{set_var, var_os},
process::ExitCode,
sync::LazyLock,
sync::{LazyLock, OnceLock},
};

const CROSS: &str = "×";
static TICK: LazyLock<ColoredString> = LazyLock::new(|| "✓".green());

pub static PARALLEL_NETWORK: OnceLock<usize> = OnceLock::new();
pub const DEFAULT_PARALLEL_NETWORK: usize = 10;

/// Indicatif themes
#[expect(clippy::expect_used)]
pub static STYLE_NO: LazyLock<ProgressStyle> = LazyLock::new(|| {
Expand Down Expand Up @@ -72,6 +75,8 @@ fn main() -> ExitCode {

let cli = Ferium::parse();

let _ = rustls::crypto::ring::default_provider().install_default();

let mut builder = tokio::runtime::Builder::new_multi_thread();
builder.enable_all();
builder.thread_name("ferium-worker");
Expand Down Expand Up @@ -138,6 +143,9 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
if let Some(key) = cli_app.curseforge_api_key {
set_var("CURSEFORGE_API_KEY", key);
}
if let Some(n) = cli_app.parallel_network {
let _ = PARALLEL_NETWORK.set(n);
}

let mut config_file = config::get_file(
&cli_app
Expand Down
10 changes: 6 additions & 4 deletions src/subcommands/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::{
download::{clean, download},
CROSS, STYLE_NO, TICK,
CROSS, DEFAULT_PARALLEL_NETWORK, PARALLEL_NETWORK, STYLE_NO, TICK,
};
use anyhow::{anyhow, bail, Result};
use colored::Colorize as _;
Expand Down Expand Up @@ -34,7 +34,9 @@ pub async fn get_platform_downloadables(profile: &Profile) -> Result<(Vec<Downlo
let mut tasks = FuturesUnordered::new();

println!("{}\n", "Determining the Latest Compatible Versions".bold());
let semaphore = Arc::new(Semaphore::new(75));
let semaphore = Arc::new(Semaphore::new(
*PARALLEL_NETWORK.get_or_init(|| DEFAULT_PARALLEL_NETWORK),
));
progress_bar
.lock()
.expect("Mutex poisoned")
Expand All @@ -47,12 +49,12 @@ pub async fn get_platform_downloadables(profile: &Profile) -> Result<(Vec<Downlo
.unwrap_or(20)
.clamp(20, 50);
for mod_ in profile.mods.clone() {
let permit = Arc::clone(&semaphore).acquire_owned().await?;
let semaphore = Arc::clone(&semaphore);
let to_download = Arc::clone(&to_download);
let progress_bar = Arc::clone(&progress_bar);

tasks.push(async move {
let _permit = permit;
let _permit = semaphore.acquire_owned().await?;
let result = mod_.fetch_download_file(profile.filters.clone()).await;
let progress_bar = progress_bar.lock().expect("Mutex poisoned");
progress_bar.inc(1);
Expand Down

0 comments on commit eb3cd33

Please sign in to comment.