Skip to content

Commit 99e6a98

Browse files
Simplify semaphore
1 parent 1b1508f commit 99e6a98

File tree

5 files changed

+21
-20
lines changed

5 files changed

+21
-20
lines changed

src/cli.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![deny(missing_docs)]
22

3-
use crate::DEFAULT_PARALLEL_NETWORK;
3+
use crate::DEFAULT_PARALLEL_TASKS;
44
use clap::{Args, Parser, Subcommand, ValueEnum, ValueHint};
55
use clap_complete::Shell;
66
use libium::config::{
@@ -18,9 +18,9 @@ pub struct Ferium {
1818
/// You can also use the environment variable `TOKIO_WORKER_THREADS`.
1919
#[clap(long, short)]
2020
pub threads: Option<usize>,
21-
/// Specify the maximum number of parallel network requests to perform.
22-
#[clap(long, short = 'p', default_value_t = DEFAULT_PARALLEL_NETWORK)]
23-
pub parallel_network: usize,
21+
/// Specify the maximum number of simultaneous parallel tasks.
22+
#[clap(long, short = 'p', default_value_t = DEFAULT_PARALLEL_TASKS)]
23+
pub parallel_tasks: usize,
2424
/// Set a GitHub personal access token for increasing the GitHub API rate limit.
2525
#[clap(long, visible_alias = "gh", env = "GITHUB_TOKEN")]
2626
pub github_token: Option<String>,

src/download.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{DEFAULT_PARALLEL_NETWORK, SEMAPHORE, STYLE_BYTE, TICK};
1+
use crate::{default_semaphore, SEMAPHORE, STYLE_BYTE, TICK};
22
use anyhow::{anyhow, bail, Error, Result};
33
use colored::Colorize as _;
44
use fs_extra::{
@@ -15,7 +15,7 @@ use std::{
1515
sync::Arc,
1616
time::Duration,
1717
};
18-
use tokio::{sync::Semaphore, task::JoinSet};
18+
use tokio::task::JoinSet;
1919

2020
/// Check the given `directory`
2121
///
@@ -118,10 +118,7 @@ pub async fn download(
118118
let output_dir = output_dir.clone();
119119

120120
tasks.spawn(async move {
121-
let _permit = SEMAPHORE
122-
.get_or_init(|| Semaphore::new(DEFAULT_PARALLEL_NETWORK))
123-
.acquire()
124-
.await?;
121+
let _permit = SEMAPHORE.get_or_init(default_semaphore).acquire().await?;
125122

126123
let (length, filename) = downloadable
127124
.download(client, &output_dir, |additional| {

src/main.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,17 @@ use std::{
4444
process::ExitCode,
4545
sync::{LazyLock, OnceLock},
4646
};
47+
use tokio::sync::Semaphore;
4748

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

51-
pub static SEMAPHORE: OnceLock<tokio::sync::Semaphore> = OnceLock::new();
52-
pub const DEFAULT_PARALLEL_NETWORK: usize = 50;
52+
pub const DEFAULT_PARALLEL_TASKS: usize = 50;
53+
pub static SEMAPHORE: OnceLock<Semaphore> = OnceLock::new();
54+
#[must_use]
55+
pub const fn default_semaphore() -> Semaphore {
56+
Semaphore::const_new(DEFAULT_PARALLEL_TASKS)
57+
}
5358

5459
/// Indicatif themes
5560
#[expect(clippy::expect_used)]
@@ -149,7 +154,8 @@ async fn actual_main(mut cli_app: Ferium) -> Result<()> {
149154
set_var("CURSEFORGE_API_KEY", key);
150155
}
151156
}
152-
let _ = SEMAPHORE.set(tokio::sync::Semaphore::new(cli_app.parallel_network));
157+
158+
let _ = SEMAPHORE.set(Semaphore::new(cli_app.parallel_tasks));
153159

154160
let config_path = &cli_app
155161
.config_file

src/subcommands/upgrade.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{
2+
default_semaphore,
23
download::{clean, download},
3-
CROSS, DEFAULT_PARALLEL_NETWORK, SEMAPHORE, STYLE_NO, TICK,
4+
CROSS, SEMAPHORE, STYLE_NO, TICK,
45
};
56
use anyhow::{anyhow, bail, Result};
67
use colored::Colorize as _;
@@ -19,7 +20,7 @@ use std::{
1920
sync::{mpsc, Arc},
2021
time::Duration,
2122
};
22-
use tokio::{sync::Semaphore, task::JoinSet};
23+
use tokio::task::JoinSet;
2324

2425
/// Get the latest compatible downloadable for the mods in `profile`
2526
///
@@ -76,10 +77,7 @@ pub async fn get_platform_downloadables(profile: &Profile) -> Result<(Vec<Downlo
7677
let progress_bar = Arc::clone(&progress_bar);
7778

7879
tasks.spawn(async move {
79-
let permit = SEMAPHORE
80-
.get_or_init(|| Semaphore::new(DEFAULT_PARALLEL_NETWORK))
81-
.acquire()
82-
.await?;
80+
let permit = SEMAPHORE.get_or_init(default_semaphore).acquire().await?;
8381

8482
let result = mod_.fetch_download_file(filters).await;
8583

src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::{
1515
const DEFAULT: Ferium = Ferium {
1616
subcommand: SubCommands::Profile { subcommand: None },
1717
threads: None,
18-
parallel_network: 10,
18+
parallel_tasks: 10,
1919
github_token: None,
2020
curseforge_api_key: None,
2121
config_file: None,

0 commit comments

Comments
 (0)