diff --git a/docs/configuration/advanced/application-yml.md b/docs/configuration/advanced/application-yml.md
index 11229262b2..432d82679e 100644
--- a/docs/configuration/advanced/application-yml.md
+++ b/docs/configuration/advanced/application-yml.md
@@ -136,6 +136,7 @@ floci:
pool-gauge-interval-seconds: 60 # Log Docker pool use every N seconds while a pool is busy; 0 = off
breaker-failure-threshold: 3 # Consecutive Docker call attempts (each retry counts) failing at the transport before Floci pings the daemon and fails calls fast if it is gone; 0 = off
breaker-probe-interval-seconds: 5 # Seconds between pings while the daemon is unreachable
+ max-concurrent-launches: 0 # Most container creates and starts at once; 0 = twice the CPUs, at least 4
docker-config-path: "" # Path to dir containing Docker's config.json (e.g. /root/.docker)
registry-credentials: [] # Per-registry explicit credentials for private registries
diff --git a/docs/configuration/docker.md b/docs/configuration/docker.md
index 412bf56081..82384ddad6 100644
--- a/docs/configuration/docker.md
+++ b/docs/configuration/docker.md
@@ -135,6 +135,18 @@ floci:
Environment variables: `FLOCI_DOCKER_BREAKER_FAILURE_THRESHOLD`, `FLOCI_DOCKER_BREAKER_PROBE_INTERVAL_SECONDS`
+### Concurrent container launches
+
+Floci sends at most `max-concurrent-launches` container creates and starts to the daemon at once, so a burst of container work (a pipeline stage starting a dozen containers, a fan-out of Lambda cold starts) reaches it a few at a time instead of all at once. The rest wait up to `connection-request-timeout-seconds` for one to finish, and a launch that waits longer fails. The default, 0, means twice the CPUs Floci sees and at least 4. Image pulls, stops, removes and every other Docker call are not limited by it:
+
+```yaml
+floci:
+ docker:
+ max-concurrent-launches: 0
+```
+
+Environment variable: `FLOCI_DOCKER_MAX_CONCURRENT_LAUNCHES`
+
## Private Registry Authentication
Any service that pulls a container image from a private registry (Lambda image functions, custom OpenSearch images, private Postgres images, etc.) needs Docker credentials. Two approaches are supported and can be combined.
@@ -383,6 +395,7 @@ caller keeps its existing failure handling after retries are exhausted.
| `FLOCI_DOCKER_MAX_CONNECTIONS` | `1024` | Connection pool size for Floci's short Docker calls |
| `FLOCI_DOCKER_BREAKER_FAILURE_THRESHOLD` | `3` | Docker call attempts in a row (each retry counts) that fail before the daemon answers, after which a failed ping makes every Docker call fail at once until the daemon answers again; `0` turns this off |
| `FLOCI_DOCKER_BREAKER_PROBE_INTERVAL_SECONDS` | `5` | Seconds between the pings that check whether an unreachable Docker daemon is back |
+| `FLOCI_DOCKER_MAX_CONCURRENT_LAUNCHES` | `0` | Most container creates and starts sent to the daemon at once; `0` means twice the CPUs Floci sees, at least 4 |
| `FLOCI_DOCKER_STREAMING_MAX_CONNECTIONS` | `3072` | Connection pool size for streams that last a container's lifetime or a long command's run. Each live Lambda container holds two (log stream and exit watcher) plus one per extension, and each CodeBuild phase, SSM command, ECS Exec session, EKS audit follower, user data script and EMR bootstrap action one |
| `FLOCI_DOCKER_REGISTRY_CREDENTIALS_0__SERVER` | _(unset)_ | Registry hostname for credential entry 0 |
| `FLOCI_DOCKER_REGISTRY_CREDENTIALS_0__USERNAME` | _(unset)_ | Username for credential entry 0 |
diff --git a/docs/configuration/environment-variables.md b/docs/configuration/environment-variables.md
index 79eea724f9..51c1148e7e 100644
--- a/docs/configuration/environment-variables.md
+++ b/docs/configuration/environment-variables.md
@@ -151,6 +151,7 @@ This foundation release provides opt-in security-group filtering for EC2 Docker
| `FLOCI_DOCKER_POOL_GAUGE_INTERVAL_SECONDS` | `60` | Seconds between the log lines that report each Docker pool's leased, available and pending connections and its open streams, written only while a pool is in use; `0` turns them off. See [Docker Configuration](docker.md#connection-pool) |
| `FLOCI_DOCKER_BREAKER_FAILURE_THRESHOLD` | `3` | Docker call attempts in a row (each retry counts) that fail before the daemon answers, after which a failed ping makes every Docker call fail at once until the daemon answers again; `0` turns this off. See [Docker Configuration](docker.md#unreachable-daemon) |
| `FLOCI_DOCKER_BREAKER_PROBE_INTERVAL_SECONDS` | `5` | Seconds between the pings that check whether an unreachable Docker daemon is back |
+| `FLOCI_DOCKER_MAX_CONCURRENT_LAUNCHES` | `0` | Most container creates and starts sent to the Docker daemon at once; `0` means twice the CPUs Floci sees, at least 4. See [Docker Configuration](docker.md#concurrent-container-launches) |
| `FLOCI_DOCKER_IMAGE_REGISTRY_BASE` | _(none)_ | Optional registry/repository base for every Docker image Floci launches. When set, `postgres:16-alpine` resolves as `
It sits in the control-plane transport, under {@link RetryingDockerHttpClient}, so a permit is held
+ * for exactly one HTTP request: never across a pull, a network attach, a file copy or a retry's backoff,
+ * and never twice by one thread, so a launch that starts a helper container inside it cannot deadlock.
+ * Every create and start goes through here, including the few call sites that bypass
+ * {@code ContainerLifecycleManager}. A call that cannot get a permit within the wait fails with
+ * {@link LaunchSlotTimeoutException}, which the retry layer does not replay.
+ */
+final class LaunchBulkheadDockerHttpClient implements DockerHttpClient {
+
+ private static final Pattern LAUNCH_ROUTE = Pattern.compile("(/v[0-9.]+)?/containers/(create|[^/]+/start)");
+
+ private final DockerHttpClient delegate;
+ private final Semaphore slots;
+ private final int size;
+ private final Duration acquireTimeout;
+
+ LaunchBulkheadDockerHttpClient(DockerHttpClient delegate, int size, Duration acquireTimeout) {
+ this.delegate = delegate;
+ this.slots = new Semaphore(size, true);
+ this.size = size;
+ this.acquireTimeout = acquireTimeout;
+ }
+
+ /** {@code floci.docker.max-concurrent-launches}: 0 means twice the CPUs Floci sees, at least 4. */
+ static int resolveSize(int configured, int availableProcessors) {
+ return configured > 0 ? configured : Math.max(4, 2 * availableProcessors);
+ }
+
+ /** Raised when no launch slot frees up within the wait; an {@link InterruptedIOException}, so it is never retried. */
+ static final class LaunchSlotTimeoutException extends InterruptedIOException {
+ LaunchSlotTimeoutException(String message) {
+ super(message);
+ }
+ }
+
+ @Override
+ public Response execute(Request request) {
+ if (!isLaunch(request)) {
+ return delegate.execute(request);
+ }
+ acquire(request);
+ try {
+ return delegate.execute(request);
+ } finally {
+ slots.release();
+ }
+ }
+
+ private void acquire(Request request) {
+ try {
+ if (!slots.tryAcquire(acquireTimeout.toMillis(), TimeUnit.MILLISECONDS)) {
+ throw new RuntimeException(new LaunchSlotTimeoutException("No container launch slot freed up within "
+ + acquireTimeout.toSeconds() + " s: " + size + " container creates and starts are already"
+ + " running (floci.docker.max-concurrent-launches); not sent: " + request.method() + " "
+ + route(request.path())));
+ }
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException("Interrupted while waiting for a container launch slot", e);
+ }
+ }
+
+ /** {@code POST /containers/create} or {@code POST /containers/{id}/start}, after an optional API version. */
+ static boolean isLaunch(Request request) {
+ return "POST".equals(request.method()) && LAUNCH_ROUTE.matcher(route(request.path())).matches();
+ }
+
+ int availableSlots() {
+ return slots.availablePermits();
+ }
+
+ /** Launches waiting for a slot; an estimate, as {@link Semaphore#getQueueLength()} is. */
+ int queuedLaunches() {
+ return slots.getQueueLength();
+ }
+
+ private static String route(String path) {
+ int query = path.indexOf('?');
+ return query < 0 ? path : path.substring(0, query);
+ }
+
+ @Override
+ public void close() throws IOException {
+ delegate.close();
+ }
+}
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index 6df36a2c61..63165eaad1 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -440,6 +440,11 @@ floci:
# here WINS over the annotation.
breaker-failure-threshold: 3
breaker-probe-interval-seconds: 5
+ # FLOCI_DOCKER_MAX_CONCURRENT_LAUNCHES: most container creates and starts sent to the daemon at
+ # once; more wait up to connection-request-timeout-seconds for one to finish. 0 = twice the CPUs
+ # Floci sees, at least 4. Keep in step with the @WithDefault on
+ # EmulatorConfig.DockerConfig.maxConcurrentLaunches: a value set here WINS over the annotation.
+ max-concurrent-launches: 0
resource-namespace: "" # FLOCI_DOCKER_RESOURCE_NAMESPACE; scopes child container/volume names
docker-config-path: ""
# extra-labels: # Extra Docker labels applied to every Floci-created container and volume
diff --git a/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java b/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java
index e98b4944ff..001523eb31 100644
--- a/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java
+++ b/src/test/java/io/floci/aws/config/ApplicationDefaultsTest.java
@@ -234,4 +234,16 @@ class ApplicationDefaultsTest {
assertEquals("5", EmulatorConfig.DockerConfig.class.getMethod("breakerProbeIntervalSeconds")
.getAnnotation(WithDefault.class).value(), "the @WithDefault must match application.yml");
}
+
+ // Catches: max-concurrent-launches missing from the shipped application.yml, or a yml value that
+ // silently disagrees with its @WithDefault fallback.
+ @Test
+ void productionConfigDeclaresTheContainerLaunchBoundAtItsDefault() throws IOException, NoSuchMethodException {
+ JsonNode launches = productionConfig().path("floci").path("docker").path("max-concurrent-launches");
+
+ assertFalse(launches.isMissingNode(), "max-concurrent-launches should be declared in application.yml");
+ assertEquals(0, launches.asInt(-1));
+ assertEquals("0", EmulatorConfig.DockerConfig.class.getMethod("maxConcurrentLaunches")
+ .getAnnotation(WithDefault.class).value(), "the @WithDefault must match application.yml");
+ }
}
diff --git a/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java b/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java
index df6b8ffc61..8724e528a0 100644
--- a/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java
+++ b/src/test/java/io/floci/aws/core/common/docker/DockerClientProducerTest.java
@@ -716,6 +716,37 @@ class DockerClientProducerTest {
assertEquals(60, DockerClientProducer.validatePoolGaugeIntervalSeconds(60));
}
+ @Test
+ void validateMaxConcurrentLaunches_negative_throwsNamingTheSetting() {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+ () -> DockerClientProducer.validateMaxConcurrentLaunches(-1));
+
+ assertThat(e.getMessage(), containsString("floci.docker.max-concurrent-launches"));
+ assertThat(e.getMessage(), containsString("FLOCI_DOCKER_MAX_CONCURRENT_LAUNCHES"));
+ }
+
+ @Test
+ void validateMaxConcurrentLaunches_zeroOrPositive_returnsItUnchanged() {
+ assertEquals(0, DockerClientProducer.validateMaxConcurrentLaunches(0));
+ assertEquals(8, DockerClientProducer.validateMaxConcurrentLaunches(8));
+ }
+
+ @Test
+ void validateBreakerFailureThreshold_negative_throwsNamingTheSetting() {
+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
+ () -> DockerClientProducer.validateBreakerFailureThreshold(-1));
+
+ assertThat(e.getMessage(), containsString("floci.docker.breaker-failure-threshold"));
+ assertThat(e.getMessage(), containsString("FLOCI_DOCKER_BREAKER_FAILURE_THRESHOLD"));
+ assertThat(e.getMessage(), containsString("must be at least 0"));
+ }
+
+ @Test
+ void validateBreakerFailureThreshold_zeroOrPositive_returnsItUnchanged() {
+ assertEquals(0, DockerClientProducer.validateBreakerFailureThreshold(0));
+ assertEquals(3, DockerClientProducer.validateBreakerFailureThreshold(3));
+ }
+
@Test
void validateImagePullTimeoutSeconds_positive_returnsItUnchanged() {
assertEquals(1, DockerClientProducer.validateImagePullTimeoutSeconds(1));
diff --git a/src/test/java/io/floci/aws/core/common/docker/LaunchBulkheadDockerHttpClientTest.java b/src/test/java/io/floci/aws/core/common/docker/LaunchBulkheadDockerHttpClientTest.java
new file mode 100644
index 0000000000..8edc180e23
--- /dev/null
+++ b/src/test/java/io/floci/aws/core/common/docker/LaunchBulkheadDockerHttpClientTest.java
@@ -0,0 +1,234 @@
+package io.floci.aws.core.common.docker;
+
+import com.github.dockerjava.transport.DockerHttpClient;
+import com.github.dockerjava.transport.DockerHttpClient.Request;
+import com.github.dockerjava.transport.DockerHttpClient.Response;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.SocketException;
+import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+import java.util.function.IntSupplier;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class LaunchBulkheadDockerHttpClientTest {
+
+ private static Request post(String path) {
+ return Request.builder().method(Request.Method.POST).path(path).build();
+ }
+
+ // Catches: the bulkhead gating calls it was never meant to (stops, exec starts, image creates) or
+ // missing a create or start behind an API version prefix.
+ @Test
+ void onlyContainerCreatesAndStartsAreLaunches() {
+ assertTrue(LaunchBulkheadDockerHttpClient.isLaunch(post("/containers/create")));
+ assertTrue(LaunchBulkheadDockerHttpClient.isLaunch(post("/v1.47/containers/create?name=floci-x")));
+ assertTrue(LaunchBulkheadDockerHttpClient.isLaunch(post("/v1.47/containers/abc123/start")));
+
+ assertFalse(LaunchBulkheadDockerHttpClient.isLaunch(post("/v1.47/containers/abc123/stop")));
+ assertFalse(LaunchBulkheadDockerHttpClient.isLaunch(post("/v1.47/exec/abc123/start")));
+ assertFalse(LaunchBulkheadDockerHttpClient.isLaunch(post("/v1.47/images/create?fromImage=busybox")));
+ assertFalse(LaunchBulkheadDockerHttpClient.isLaunch(post("/v1.47/images/quay.io/containers/create")));
+ assertFalse(LaunchBulkheadDockerHttpClient.isLaunch(
+ Request.builder().method(Request.Method.GET).path("/v1.47/containers/abc123/json").build()));
+ }
+
+ // Catches: a burst of launches reaching the daemon all at once.
+ @Test
+ void runsAtMostItsSizeOfLaunchesAtOnceAndCompletesThemAll() throws Exception {
+ CountDownLatch release = new CountDownLatch(1);
+ AtomicInteger inFlight = new AtomicInteger();
+ AtomicInteger maxInFlight = new AtomicInteger();
+ FakeTransport transport = new FakeTransport(request -> {
+ maxInFlight.accumulateAndGet(inFlight.incrementAndGet(), Math::max);
+ try {
+ release.await(5, TimeUnit.SECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ } finally {
+ inFlight.decrementAndGet();
+ }
+ return new StatusResponse(201);
+ });
+ LaunchBulkheadDockerHttpClient bulkhead = new LaunchBulkheadDockerHttpClient(transport, 2, Duration.ofSeconds(10));
+
+ ExecutorService pool = Executors.newFixedThreadPool(5);
+ try {
+ List