Skip to content

Commit

Permalink
better parallelism
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAlan404 committed Jun 20, 2024
1 parent 3c740b6 commit cfa111f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
31 changes: 30 additions & 1 deletion Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mcman"
version = "0.4.5"
version = "0.5.0"
edition = "2021"
rust-version = "1.75"
repository = "https://github.com/ParadigmMC/mcman"
Expand Down Expand Up @@ -62,4 +62,5 @@ cliclack = "0.2.5"
os_info = { version = "3.7", default-features = false }
lazy_static = "1.4"
bytes = "1.6"
murmur2 = "0.1.0"
murmur2 = "0.1"
futures = "0.3"
24 changes: 12 additions & 12 deletions src/api/app/actions/build/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{path::Path, sync::Arc};
use futures::stream::{self, StreamExt, TryStreamExt};

use anyhow::Result;

Expand All @@ -23,19 +24,18 @@ impl App {
let addons = self.collect_addons().await?;
let base = Arc::new(base.to_owned());

let mut handles = vec![];
const MAX_CONCURRENT_TASKS: usize = 20;

Check warning on line 27 in src/api/app/actions/build/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

adding items after statements is confusing, since items exist from the start of the scope

warning: adding items after statements is confusing, since items exist from the start of the scope --> src/api/app/actions/build/mod.rs:27:9 | 27 | const MAX_CONCURRENT_TASKS: usize = 20; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#items_after_statements = note: `-W clippy::items-after-statements` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::items_after_statements)]`

for addon in addons {
let app = self.clone();
let base = base.clone();
handles.push(tokio::spawn(async move {
app.action_install_addon(&base, &addon).await
}));
}

for handle in handles {
handle.await??;
}
stream::iter(addons).map(Ok).try_for_each_concurrent(
Some(MAX_CONCURRENT_TASKS),
move |addon| {
let app = self.clone();
let base = base.clone();
async move {
app.action_install_addon(&base, &addon).await
}
}
).await?;

Ok(())
}
Expand Down
15 changes: 15 additions & 0 deletions src/api/sources/modrinth/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use models::{ModrinthFile, ModrinthProject, ModrinthVersion};
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -45,7 +47,20 @@ impl<'a> ModrinthAPI<'a> {
}

pub async fn get_id(&self, slug: &str) -> Result<String> {
let path = "modrinth/ids.json";
let store = self.0.cache.try_get_json::<HashMap<String, String>>(path)?;

if let Some(id) = store.as_ref().map(|ids| ids.get(slug)).flatten() {

Check failure on line 53 in src/api/sources/modrinth/mod.rs

View workflow job for this annotation

GitHub Actions / clippy

called `map(..).flatten()` on `Option`

error: called `map(..).flatten()` on `Option` --> src/api/sources/modrinth/mod.rs:53:42 | 53 | if let Some(id) = store.as_ref().map(|ids| ids.get(slug)).flatten() { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `and_then` and remove the `.flatten()`: `and_then(|ids| ids.get(slug))` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten = note: `-D clippy::map-flatten` implied by `-D clippy::all` = help: to override `-D clippy::all` add `#[allow(clippy::map_flatten)]`
return Ok(id.to_owned());
}

let res: ModrinthProjectCheckResponse = self.fetch_api(format!("project/{slug}/check")).await?;

if let Some(mut ids) = store {
ids.insert(slug.to_owned(), res.id.clone());
self.0.cache.try_write_json(path, &ids)?;
}

Ok(res.id)
}

Expand Down

0 comments on commit cfa111f

Please sign in to comment.