@@ -7,13 +7,15 @@ use dashmap::DashMap;
77use reqwest:: Client ;
88use serde:: Deserialize ;
99use tokio:: sync:: Semaphore ;
10+ use tokio:: time:: timeout;
1011
1112use crate :: bundle:: VersionedModule ;
1213use crate :: utils:: { read_from_file, write_to_file} ;
1314
1415const FETCH_MODULE_COOLDOWN : Duration = Duration :: from_secs ( 600 ) ;
16+
1517static 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