@@ -3,7 +3,6 @@ use futures::{
3
3
stream:: BoxStream ,
4
4
Future , FutureExt , Stream , StreamExt ,
5
5
} ;
6
- use once_cell:: sync:: Lazy ;
7
6
use serde:: Deserialize ;
8
7
use snafu:: prelude:: * ;
9
8
use std:: {
@@ -821,29 +820,60 @@ enum DemultiplexCommand {
821
820
ListenOnce ( JobId , oneshot:: Sender < WorkerMessage > ) ,
822
821
}
823
822
823
+ #[ derive( Debug , Copy , Clone ) ]
824
+ pub struct CoordinatorId {
825
+ start : u64 ,
826
+ id : u64 ,
827
+ }
828
+
824
829
/// Enforces a limited number of concurrent `Coordinator`s.
825
830
#[ derive( Debug ) ]
826
831
pub struct CoordinatorFactory {
827
832
semaphore : Arc < Semaphore > ,
833
+
834
+ start : u64 ,
835
+ id : AtomicU64 ,
828
836
}
829
837
830
838
impl CoordinatorFactory {
831
839
pub fn new ( maximum : usize ) -> Self {
840
+ let semaphore = Arc :: new ( Semaphore :: new ( maximum) ) ;
841
+
842
+ let now = std:: time:: SystemTime :: now ( ) ;
843
+ let start = now
844
+ . duration_since ( std:: time:: UNIX_EPOCH )
845
+ . unwrap_or_default ( )
846
+ . as_secs ( ) ;
847
+
848
+ let id = AtomicU64 :: new ( 0 ) ;
849
+
832
850
Self {
833
- semaphore : Arc :: new ( Semaphore :: new ( maximum) ) ,
851
+ semaphore,
852
+ start,
853
+ id,
834
854
}
835
855
}
836
856
837
- pub async fn build < B > ( & self , backend : B ) -> LimitedCoordinator < B >
857
+ fn next_id ( & self ) -> CoordinatorId {
858
+ let start = self . start ;
859
+ let id = self . id . fetch_add ( 1 , Ordering :: SeqCst ) ;
860
+
861
+ CoordinatorId { start, id }
862
+ }
863
+
864
+ pub async fn build < B > ( & self ) -> LimitedCoordinator < B >
838
865
where
839
- B : Backend ,
866
+ B : Backend + From < CoordinatorId > ,
840
867
{
841
868
let semaphore = self . semaphore . clone ( ) ;
842
869
let permit = semaphore
843
870
. acquire_owned ( )
844
871
. await
845
872
. expect ( "Unable to acquire permit" ) ;
846
873
874
+ let id = self . next_id ( ) ;
875
+ let backend = B :: from ( id) ;
876
+
847
877
let coordinator = Coordinator :: new ( backend) ;
848
878
849
879
LimitedCoordinator {
@@ -1149,12 +1179,6 @@ where
1149
1179
}
1150
1180
}
1151
1181
1152
- impl Coordinator < DockerBackend > {
1153
- pub fn new_docker ( ) -> Self {
1154
- Self :: new ( DockerBackend ( ( ) ) )
1155
- }
1156
- }
1157
-
1158
1182
#[ derive( Debug ) ]
1159
1183
struct Container {
1160
1184
task : JoinHandle < Result < ( ) > > ,
@@ -2581,24 +2605,26 @@ fn basic_secure_docker_command() -> Command {
2581
2605
)
2582
2606
}
2583
2607
2584
- static DOCKER_BACKEND_START : Lazy < u64 > = Lazy :: new ( || {
2585
- use std:: time;
2586
-
2587
- let now = time:: SystemTime :: now ( ) ;
2588
- now. duration_since ( time:: UNIX_EPOCH )
2589
- . unwrap_or_default ( )
2590
- . as_secs ( )
2591
- } ) ;
2592
-
2593
- static DOCKER_BACKEND_ID : AtomicU64 = AtomicU64 :: new ( 0 ) ;
2608
+ pub struct DockerBackend {
2609
+ id : CoordinatorId ,
2610
+ instance : AtomicU64 ,
2611
+ }
2594
2612
2595
- pub struct DockerBackend ( ( ) ) ;
2613
+ impl From < CoordinatorId > for DockerBackend {
2614
+ fn from ( id : CoordinatorId ) -> Self {
2615
+ Self {
2616
+ id,
2617
+ instance : Default :: default ( ) ,
2618
+ }
2619
+ }
2620
+ }
2596
2621
2597
2622
impl DockerBackend {
2598
2623
fn next_name ( & self ) -> String {
2599
- let start = * DOCKER_BACKEND_START ;
2600
- let id = DOCKER_BACKEND_ID . fetch_add ( 1 , Ordering :: SeqCst ) ;
2601
- format ! ( "playground-{start}-{id}" )
2624
+ let CoordinatorId { start, id } = self . id ;
2625
+ let instance = self . instance . fetch_add ( 1 , Ordering :: SeqCst ) ;
2626
+
2627
+ format ! ( "playground-{start}-{id}-{instance}" )
2602
2628
}
2603
2629
}
2604
2630
@@ -2758,6 +2784,7 @@ fn spawn_io_queue(stdin: ChildStdin, stdout: ChildStdout, token: CancellationTok
2758
2784
mod tests {
2759
2785
use assertables:: * ;
2760
2786
use futures:: future:: { join, try_join_all} ;
2787
+ use once_cell:: sync:: Lazy ;
2761
2788
use std:: { env, sync:: Once } ;
2762
2789
use tempdir:: TempDir ;
2763
2790
@@ -2781,8 +2808,8 @@ mod tests {
2781
2808
project_dir : TempDir ,
2782
2809
}
2783
2810
2784
- impl TestBackend {
2785
- fn new ( ) -> Self {
2811
+ impl From < CoordinatorId > for TestBackend {
2812
+ fn from ( _id : CoordinatorId ) -> Self {
2786
2813
static COMPILE_WORKER_ONCE : Once = Once :: new ( ) ;
2787
2814
2788
2815
COMPILE_WORKER_ONCE . call_once ( || {
@@ -2839,12 +2866,12 @@ mod tests {
2839
2866
static TEST_COORDINATOR_FACTORY : Lazy < CoordinatorFactory > =
2840
2867
Lazy :: new ( || CoordinatorFactory :: new ( * MAX_CONCURRENT_TESTS ) ) ;
2841
2868
2842
- async fn new_coordinator_test ( ) -> LimitedCoordinator < impl Backend > {
2843
- TEST_COORDINATOR_FACTORY . build ( TestBackend :: new ( ) ) . await
2869
+ async fn new_coordinator_test ( ) -> LimitedCoordinator < TestBackend > {
2870
+ TEST_COORDINATOR_FACTORY . build ( ) . await
2844
2871
}
2845
2872
2846
- async fn new_coordinator_docker ( ) -> LimitedCoordinator < impl Backend > {
2847
- TEST_COORDINATOR_FACTORY . build ( DockerBackend ( ( ) ) ) . await
2873
+ async fn new_coordinator_docker ( ) -> LimitedCoordinator < DockerBackend > {
2874
+ TEST_COORDINATOR_FACTORY . build ( ) . await
2848
2875
}
2849
2876
2850
2877
async fn new_coordinator ( ) -> LimitedCoordinator < impl Backend > {
0 commit comments