@@ -861,60 +861,23 @@ impl CoordinatorFactory {
861
861
CoordinatorId { start, id }
862
862
}
863
863
864
- pub async fn build < B > ( & self ) -> LimitedCoordinator < B >
864
+ pub async fn build < B > ( & self ) -> Coordinator < B >
865
865
where
866
866
B : Backend + From < CoordinatorId > ,
867
867
{
868
868
let semaphore = self . semaphore . clone ( ) ;
869
- let permit = semaphore
870
- . acquire_owned ( )
871
- . await
872
- . expect ( "Unable to acquire permit" ) ;
873
869
874
870
let id = self . next_id ( ) ;
875
871
let backend = B :: from ( id) ;
876
872
877
- let coordinator = Coordinator :: new ( backend) ;
878
-
879
- LimitedCoordinator {
880
- coordinator,
881
- _permit : permit,
882
- }
883
- }
884
- }
885
-
886
- pub struct LimitedCoordinator < T > {
887
- coordinator : Coordinator < T > ,
888
- _permit : OwnedSemaphorePermit ,
889
- }
890
-
891
- impl < T > LimitedCoordinator < T >
892
- where
893
- T : Backend ,
894
- {
895
- pub async fn shutdown ( self ) -> Result < T > {
896
- self . coordinator . shutdown ( ) . await
897
- }
898
- }
899
-
900
- impl < T > ops:: Deref for LimitedCoordinator < T > {
901
- type Target = Coordinator < T > ;
902
-
903
- fn deref ( & self ) -> & Self :: Target {
904
- & self . coordinator
905
- }
906
- }
907
-
908
- impl < T > ops:: DerefMut for LimitedCoordinator < T > {
909
- fn deref_mut ( & mut self ) -> & mut Self :: Target {
910
- & mut self . coordinator
873
+ Coordinator :: new ( semaphore, backend)
911
874
}
912
875
}
913
876
914
877
#[ derive( Debug ) ]
915
878
pub struct Coordinator < B > {
879
+ semaphore : Arc < Semaphore > ,
916
880
backend : B ,
917
- // Consider making these lazily-created and/or idly time out
918
881
stable : OnceCell < Container > ,
919
882
beta : OnceCell < Container > ,
920
883
nightly : OnceCell < Container > ,
@@ -934,10 +897,11 @@ impl<B> Coordinator<B>
934
897
where
935
898
B : Backend ,
936
899
{
937
- pub fn new ( backend : B ) -> Self {
900
+ pub fn new ( semaphore : Arc < Semaphore > , backend : B ) -> Self {
938
901
let token = CancellationToken :: new ( ) ;
939
902
940
903
Self {
904
+ semaphore,
941
905
backend,
942
906
stable : OnceCell :: new ( ) ,
943
907
beta : OnceCell :: new ( ) ,
@@ -1174,13 +1138,18 @@ where
1174
1138
} ;
1175
1139
1176
1140
container
1177
- . get_or_try_init ( || Container :: new ( channel, self . token . clone ( ) , & self . backend ) )
1141
+ . get_or_try_init ( || {
1142
+ let semaphore = self . semaphore . clone ( ) ;
1143
+ let token = self . token . clone ( ) ;
1144
+ Container :: new ( channel, semaphore, token, & self . backend )
1145
+ } )
1178
1146
. await
1179
1147
}
1180
1148
}
1181
1149
1182
1150
#[ derive( Debug ) ]
1183
1151
struct Container {
1152
+ permit : OwnedSemaphorePermit ,
1184
1153
task : JoinHandle < Result < ( ) > > ,
1185
1154
kill_child : Option < Command > ,
1186
1155
modify_cargo_toml : ModifyCargoToml ,
@@ -1190,9 +1159,15 @@ struct Container {
1190
1159
impl Container {
1191
1160
async fn new (
1192
1161
channel : Channel ,
1162
+ semaphore : Arc < Semaphore > ,
1193
1163
token : CancellationToken ,
1194
1164
backend : & impl Backend ,
1195
1165
) -> Result < Self > {
1166
+ let permit = semaphore
1167
+ . acquire_owned ( )
1168
+ . await
1169
+ . context ( AcquirePermitSnafu ) ?;
1170
+
1196
1171
let ( mut child, kill_child, stdin, stdout) = backend. run_worker_in_background ( channel) ?;
1197
1172
let IoQueue {
1198
1173
mut tasks,
@@ -1231,6 +1206,7 @@ impl Container {
1231
1206
. context ( CouldNotLoadCargoTomlSnafu ) ?;
1232
1207
1233
1208
Ok ( Container {
1209
+ permit,
1234
1210
task,
1235
1211
kill_child,
1236
1212
modify_cargo_toml,
@@ -1907,6 +1883,7 @@ impl Container {
1907
1883
1908
1884
async fn shutdown ( self ) -> Result < ( ) > {
1909
1885
let Self {
1886
+ permit,
1910
1887
task,
1911
1888
kill_child,
1912
1889
modify_cargo_toml,
@@ -1927,7 +1904,10 @@ impl Container {
1927
1904
. context ( KillWorkerSnafu ) ?;
1928
1905
}
1929
1906
1930
- task. await . context ( ContainerTaskPanickedSnafu ) ?
1907
+ let r = task. await ;
1908
+ drop ( permit) ;
1909
+
1910
+ r. context ( ContainerTaskPanickedSnafu ) ?
1931
1911
}
1932
1912
}
1933
1913
@@ -2705,6 +2685,9 @@ pub enum Error {
2705
2685
2706
2686
#[ snafu( display( "Unable to load original Cargo.toml" ) ) ]
2707
2687
CouldNotLoadCargoToml { source : ModifyCargoTomlError } ,
2688
+
2689
+ #[ snafu( display( "Could not acquire a semaphore permit" ) ) ]
2690
+ AcquirePermit { source : tokio:: sync:: AcquireError } ,
2708
2691
}
2709
2692
2710
2693
struct IoQueue {
@@ -2866,15 +2849,15 @@ mod tests {
2866
2849
static TEST_COORDINATOR_FACTORY : Lazy < CoordinatorFactory > =
2867
2850
Lazy :: new ( || CoordinatorFactory :: new ( * MAX_CONCURRENT_TESTS ) ) ;
2868
2851
2869
- async fn new_coordinator_test ( ) -> LimitedCoordinator < TestBackend > {
2852
+ async fn new_coordinator_test ( ) -> Coordinator < TestBackend > {
2870
2853
TEST_COORDINATOR_FACTORY . build ( ) . await
2871
2854
}
2872
2855
2873
- async fn new_coordinator_docker ( ) -> LimitedCoordinator < DockerBackend > {
2856
+ async fn new_coordinator_docker ( ) -> Coordinator < DockerBackend > {
2874
2857
TEST_COORDINATOR_FACTORY . build ( ) . await
2875
2858
}
2876
2859
2877
- async fn new_coordinator ( ) -> LimitedCoordinator < impl Backend > {
2860
+ async fn new_coordinator ( ) -> Coordinator < impl Backend > {
2878
2861
#[ cfg( not( force_docker) ) ]
2879
2862
{
2880
2863
new_coordinator_test ( ) . await
0 commit comments