Skip to content

Simplify container acquisition / notification code #1168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions compiler/base/orchestrator/src/coordinator/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::{
Arc,
},
};
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use tokio::sync::{OwnedSemaphorePermit, Semaphore, TryAcquireError};

use super::{ContainerPermit, ProcessPermit, ResourceLimits, ResourceResult};
use super::{ContainerPermit, ProcessPermit, ResourceError, ResourceLimits, ResourceResult};

/// Describe how the resource was (or was not) acquired.
#[derive(Debug, Copy, Clone, PartialEq)]
Expand Down Expand Up @@ -187,21 +187,17 @@ where
// occur when we are already at the upper bounds of our
// limits. In those cases, freeing an extra container or
// two shouldn't be the worst thing.
let container_permit = {
let fallback = {
let container_semaphore = container_semaphore.clone();
async {
container_request_semaphore.add_permits(1);
container_semaphore.acquire_owned().await
}
};

tokio::select! {
biased;

permit = container_semaphore.acquire_owned() => permit,
permit = fallback => permit,

let container_permit = match container_semaphore.clone().try_acquire_owned() {
Ok(permit) => Ok(permit),
Err(TryAcquireError::NoPermits) => {
container_request_semaphore.add_permits(1);
container_semaphore
.acquire_owned()
.await
.map_err(ResourceError::from)
}
Err(e) => Err(e.into()),
};

let container_permit = guard.complete(container_permit)?;
Expand Down