1+ use std:: fmt;
12use std:: num:: NonZeroUsize ;
3+ use std:: sync:: Arc ;
4+
5+ use tokio:: sync:: Semaphore ;
26
37/// Concurrency limit settings.
4- #[ derive( Copy , Clone , Debug ) ]
8+ // TODO(konsti): We should find a pattern that doesn't require having both semaphores and counts.
9+ #[ derive( Clone ) ]
510pub struct Concurrency {
611 /// The maximum number of concurrent downloads.
712 ///
@@ -15,22 +20,45 @@ pub struct Concurrency {
1520 ///
1621 /// Note this value must be non-zero.
1722 pub installs : usize ,
23+ /// A global semaphore to limit the number of concurrent downloads.
24+ pub downloads_semaphore : Arc < Semaphore > ,
25+ /// A global semaphore to limit the number of concurrent builds.
26+ pub builds_semaphore : Arc < Semaphore > ,
27+ }
28+
29+ /// Custom `Debug` to hide semaphore fields from `--show-settings` output.
30+ #[ expect( clippy:: missing_fields_in_debug) ]
31+ impl fmt:: Debug for Concurrency {
32+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
33+ f. debug_struct ( "Concurrency" )
34+ . field ( "downloads" , & self . downloads )
35+ . field ( "builds" , & self . builds )
36+ . field ( "installs" , & self . installs )
37+ . finish ( )
38+ }
1839}
1940
2041impl Default for Concurrency {
2142 fn default ( ) -> Self {
22- Self {
23- downloads : Self :: DEFAULT_DOWNLOADS ,
24- builds : Self :: threads ( ) ,
25- installs : Self :: threads ( ) ,
26- }
43+ Self :: new ( Self :: DEFAULT_DOWNLOADS , Self :: threads ( ) , Self :: threads ( ) )
2744 }
2845}
2946
3047impl Concurrency {
3148 // The default concurrent downloads limit.
3249 pub const DEFAULT_DOWNLOADS : usize = 50 ;
3350
51+ /// Create a new [`Concurrency`] with the given limits.
52+ pub fn new ( downloads : usize , builds : usize , installs : usize ) -> Self {
53+ Self {
54+ downloads,
55+ builds,
56+ installs,
57+ downloads_semaphore : Arc :: new ( Semaphore :: new ( downloads) ) ,
58+ builds_semaphore : Arc :: new ( Semaphore :: new ( builds) ) ,
59+ }
60+ }
61+
3462 // The default concurrent builds and install limit.
3563 pub fn threads ( ) -> usize {
3664 std:: thread:: available_parallelism ( )
0 commit comments