Skip to content

Commit 26e3670

Browse files
committed
Simplify container acquisition / notification code
1 parent 397bd40 commit 26e3670

File tree

1 file changed

+12
-16
lines changed
  • compiler/base/orchestrator/src/coordinator

1 file changed

+12
-16
lines changed

compiler/base/orchestrator/src/coordinator/limits.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::{
66
Arc,
77
},
88
};
9-
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
9+
use tokio::sync::{OwnedSemaphorePermit, Semaphore, TryAcquireError};
1010

11-
use super::{ContainerPermit, ProcessPermit, ResourceLimits, ResourceResult};
11+
use super::{ContainerPermit, ProcessPermit, ResourceError, ResourceLimits, ResourceResult};
1212

1313
/// Describe how the resource was (or was not) acquired.
1414
#[derive(Debug, Copy, Clone, PartialEq)]
@@ -187,21 +187,17 @@ where
187187
// occur when we are already at the upper bounds of our
188188
// limits. In those cases, freeing an extra container or
189189
// two shouldn't be the worst thing.
190-
let container_permit = {
191-
let fallback = {
192-
let container_semaphore = container_semaphore.clone();
193-
async {
194-
container_request_semaphore.add_permits(1);
195-
container_semaphore.acquire_owned().await
196-
}
197-
};
198-
199-
tokio::select! {
200-
biased;
201-
202-
permit = container_semaphore.acquire_owned() => permit,
203-
permit = fallback => permit,
190+
191+
let container_permit = match container_semaphore.clone().try_acquire_owned() {
192+
Ok(permit) => Ok(permit),
193+
Err(TryAcquireError::NoPermits) => {
194+
container_request_semaphore.add_permits(1);
195+
container_semaphore
196+
.acquire_owned()
197+
.await
198+
.map_err(ResourceError::from)
204199
}
200+
Err(e) => Err(e.into()),
205201
};
206202

207203
let container_permit = guard.complete(container_permit)?;

0 commit comments

Comments
 (0)