Skip to content

Commit e752681

Browse files
committed
⚡️ feat: improve limits on Modrinth requests
1 parent 04d0a1b commit e752681

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bookshelf-api"
3-
version = "1.3.0"
3+
version = "1.3.1"
44
edition = "2024"
55
authors = ["Aksiome"]
66
description = "REST API for the Bookshelf project"

src/bundle/fetch.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ use dashmap::DashMap;
77
use reqwest::Client;
88
use serde::Deserialize;
99
use tokio::sync::Semaphore;
10+
use tokio::time::timeout;
1011

1112
use crate::bundle::VersionedModule;
1213
use crate::utils::{read_from_file, write_to_file};
1314

1415
const FETCH_MODULE_COOLDOWN: Duration = Duration::from_secs(600);
16+
1517
static FETCH_MODULE_LAST: OnceLock<DashMap<String, Instant>> = OnceLock::new();
16-
static SEM: OnceLock<Arc<Semaphore>> = OnceLock::new();
18+
static SEMAPHORE: OnceLock<Arc<Semaphore>> = OnceLock::new();
1719

1820

1921
#[derive(Clone, Debug, Deserialize)]
@@ -47,16 +49,22 @@ pub async fn fetch_module(
4749
let cache_path = format!("cache/{}/{}.zip", module.version, module.id);
4850
if let Ok(bytes) = read_from_file(&cache_path).await {
4951
let now = Instant::now();
50-
if FETCH_MODULE_LAST.get_or_init(DashMap::new).get(&cache_path).is_none_or(
51-
|last| now.duration_since(*last.value()) > FETCH_MODULE_COOLDOWN
52-
) {
53-
let sem = SEM.get_or_init(|| Arc::new(Semaphore::new(3))).clone();
52+
let map = FETCH_MODULE_LAST.get_or_init(DashMap::new);
53+
54+
if map.get(&cache_path).is_none_or(|last| now.duration_since(*last.value()) > FETCH_MODULE_COOLDOWN) {
55+
let sem = SEMAPHORE.get_or_init(|| Arc::new(Semaphore::new(3))).clone();
56+
map.insert(cache_path.clone(), now);
5457

5558
tokio::spawn(async move {
56-
let _permit = sem.acquire().await.unwrap();
57-
if let Ok(url) = fetch_module_url_from_modrinth(&client, &module).await {
58-
let _ = client.get(url).send().await;
59-
FETCH_MODULE_LAST.get_or_init(DashMap::new).insert(cache_path, now);
59+
if let Ok(_permit) = sem.acquire().await {
60+
let url = match fetch_module_url_from_modrinth(&client, &module).await {
61+
Ok(url) => url,
62+
Err(_) => return,
63+
};
64+
65+
if let Ok(Ok(resp)) = timeout(Duration::from_secs(5), client.get(url).send()).await {
66+
let _ = resp.bytes().await;
67+
}
6068
}
6169
});
6270
}

0 commit comments

Comments
 (0)