Light, fluffy, and always free - The AWS Local Emulator alternative
feat(core): make the Docker call timeout configurable (#5431)
Hector Ventura98100054a84e
diff --git a/docs/configuration/docker.md b/docs/configuration/docker.mdindex 7651d67ab1..f70f50e9ef 100644--- a/docs/configuration/docker.md+++ b/docs/configuration/docker.md@@ -82,7 +82,15 @@ floci:Environment variable: `FLOCI_DOCKER_CONNECTION_REQUEST_TIMEOUT_SECONDS`-A Docker call that gets no response within 5 minutes fails, on Unix-socket and TCP hosts alike. Requests that follow a container for as long as it runs (container waits, exec and attach output, followed logs, streamed stats, daemon events, image pulls and builds) have no such limit, since they can stay silent while healthy.+A Docker call that gets no response within `api-timeout-seconds` fails, on Unix-socket and TCP hosts alike, so a hung daemon cannot block a request indefinitely. The limit applies to each attempt: a control-plane request that is safe to repeat is tried up to six times, so against a silent daemon it can take about six times the limit, plus backoff, to fail. It defaults to 300 seconds (5 minutes) for both pools and must be at least 1, since httpclient5 reads 0 as no limit. Requests that follow a container for as long as it runs (container waits, exec and attach output, followed logs, streamed stats, daemon events, image pulls and builds) have no such limit, since they can stay silent while healthy:++```yaml+floci:+ docker:+ api-timeout-seconds: 300+```++Environment variable: `FLOCI_DOCKER_API_TIMEOUT_SECONDS`## Private Registry Authenticationdiff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.javaindex ba4553bdad..4f8d5d55db 100644--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java@@ -3540,6 +3540,16 @@ public interface EmulatorConfig {@WithDefault("30")int connectionRequestTimeoutSeconds();+ /**+ * How long a Docker call waits for the daemon's response before it fails, in seconds, for+ * both pools. The limit is per attempt: a control-plane request that is safe to repeat is+ * tried up to six times. Requests that hold a stream open for as long as a container runs (container+ * waits, exec and attach output, followed logs, streamed stats, events, image pulls and+ * builds) are exempt, since they can stay silent while healthy.+ */+ @WithDefault("300")+ int apiTimeoutSeconds();+/*** Optional namespace inserted into Floci-managed child container and volume names.* Useful when multiple Floci processes share one Docker daemon.diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.javaindex 67b01c3157..640ba4b044 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducer.java@@ -419,6 +419,10 @@ public class DockerClientProducer {"FLOCI_DOCKER_CONNECTION_REQUEST_TIMEOUT_SECONDS");}+ static int validateApiTimeoutSeconds(int seconds) {+ return requireAtLeastOne(seconds, "floci.docker.api-timeout-seconds", "FLOCI_DOCKER_API_TIMEOUT_SECONDS");+ }+private static int requireAtLeastOne(int value, String setting, String envVar) {if (value < 1) {throw new IllegalArgumentException(setting + " (" + envVar + ") must be at least 1, got: " + value);@@ -497,18 +501,19 @@ public class DockerClientProducer {role, maxConnections, clientConfig.getDockerHost());return DockerClientImpl.getInstance(clientConfig, wrapForRole(newHttpClient(clientConfig, maxConnections,+ Duration.ofSeconds(validateApiTimeoutSeconds(config.docker().apiTimeoutSeconds())),Duration.ofSeconds(validateConnectionRequestTimeoutSeconds(config.docker().connectionRequestTimeoutSeconds()))), role));}static FlociDockerHttpClient newHttpClient(DefaultDockerClientConfig clientConfig, int maxConnections,- Duration connectionRequestTimeout) {+ Duration apiTimeout, Duration connectionRequestTimeout) {return new FlociDockerHttpClient.Builder().dockerHost(clientConfig.getDockerHost()).sslConfig(clientConfig.getSSLConfig()).maxConnections(maxConnections).connectionTimeout(Duration.ofSeconds(30))- .responseTimeout(Duration.ofMinutes(5))+ .responseTimeout(apiTimeout).connectionRequestTimeout(connectionRequestTimeout).build();}diff --git a/src/main/resources/application.yml b/src/main/resources/application.ymlindex e7eca24d74..bc36c96f70 100644--- a/src/main/resources/application.yml+++ b/src/main/resources/application.yml@@ -326,6 +326,11 @@ floci:# connection before failing, for both pools. Keep in step with the @WithDefault on# EmulatorConfig.DockerConfig.connectionRequestTimeoutSeconds: a value set here WINS over the annotation.connection-request-timeout-seconds: 30+ # FLOCI_DOCKER_API_TIMEOUT_SECONDS: how long a Docker call waits for the daemon's response before+ # failing; long-lived streams (waits, followed logs, exec output, pulls) are exempt. It applies per+ # attempt: a control-plane request that is safe to repeat is tried up to six times. Keep in step with+ # the @WithDefault on EmulatorConfig.DockerConfig.apiTimeoutSeconds: a value set here WINS over the annotation.+ api-timeout-seconds: 300resource-namespace: "" # FLOCI_DOCKER_RESOURCE_NAMESPACE; scopes child container/volume namesdocker-config-path: ""# extra-labels: # Extra Docker labels applied to every Floci-created container and volumediff --git a/src/test/java/io/github/hectorvent/floci/config/ApplicationDefaultsTest.java b/src/test/java/io/github/hectorvent/floci/config/ApplicationDefaultsTest.javaindex 9ebaa6154a..7acae74799 100644--- a/src/test/java/io/github/hectorvent/floci/config/ApplicationDefaultsTest.java+++ b/src/test/java/io/github/hectorvent/floci/config/ApplicationDefaultsTest.java@@ -172,4 +172,23 @@ class ApplicationDefaultsTest {assertEquals("30", fallback.value(),"the @WithDefault must match application.yml, which wins over it");}++ // Catches: api-timeout-seconds missing from the shipped application.yml, or a yml value that+ // silently disagrees with the @WithDefault fallback.+ @Test+ void productionConfigDeclaresTheDockerApiTimeoutAtItsDefault() throws IOException, NoSuchMethodException {+ JsonNode apiTimeout = new YAMLMapper()+ .readTree(Path.of("src/main/resources/application.yml").toFile())+ .path("floci").path("docker").path("api-timeout-seconds");++ assertFalse(apiTimeout.isMissingNode(), "api-timeout-seconds should be declared in application.yml");+ assertEquals(300, apiTimeout.asInt());++ WithDefault fallback = EmulatorConfig.DockerConfig.class+ .getMethod("apiTimeoutSeconds")+ .getAnnotation(WithDefault.class);+ assertNotNull(fallback, "apiTimeoutSeconds should declare a fallback default");+ assertEquals("300", fallback.value(),+ "the @WithDefault must match application.yml, which wins over it");+ }}diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.javaindex 90042b9551..de31b4616b 100644--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/DockerClientProducerTest.java@@ -274,6 +274,7 @@ class DockerClientProducerTest {when(docker.maxConnections()).thenReturn(100);when(docker.streamingMaxConnections()).thenReturn(512);when(docker.connectionRequestTimeoutSeconds()).thenReturn(30);+ when(docker.apiTimeoutSeconds()).thenReturn(300);DockerClientProducer producer = new DockerClientProducer(config);@@ -689,6 +690,23 @@ class DockerClientProducerTest {assertEquals(30, DockerClientProducer.validateConnectionRequestTimeoutSeconds(30));}+ // Catches: a zero response timeout reaching httpclient5, which reads it as no timeout, so a+ // hung daemon would hold a call forever.+ @ParameterizedTest+ @ValueSource(ints = {0, -1})+ void validateApiTimeoutSeconds_belowOne_throwsNamingTheSetting(int seconds) {+ IllegalArgumentException e = assertThrows(IllegalArgumentException.class,+ () -> DockerClientProducer.validateApiTimeoutSeconds(seconds));++ assertThat(e.getMessage(), containsString("floci.docker.api-timeout-seconds"));+ }++ @Test+ void validateApiTimeoutSeconds_positive_returnsItUnchanged() {+ assertEquals(1, DockerClientProducer.validateApiTimeoutSeconds(1));+ assertEquals(300, DockerClientProducer.validateApiTimeoutSeconds(300));+ }+/*** The pool size comes from {@code floci.docker.max-connections}. A followed log stream holds* its connection until the container exits, and this fake daemon never answers, so with a@@ -717,6 +735,7 @@ class DockerClientProducerTest {when(docker.dockerConfigPath()).thenReturn(Optional.empty());when(docker.maxConnections()).thenReturn(2);when(docker.connectionRequestTimeoutSeconds()).thenReturn(30);+ when(docker.apiTimeoutSeconds()).thenReturn(300);DockerClient client = new DockerClientProducer(config).dockerClient();try {@@ -761,7 +780,7 @@ class DockerClientProducerTest {// An HTTP client that actually uses the SSL config must read the certificates, so// unreadable ones fail the build; one that ignores the config builds silently.assertThrows(RuntimeException.class,- () -> DockerClientProducer.newHttpClient(tlsConfig, 10, Duration.ofSeconds(30)));+ () -> DockerClientProducer.newHttpClient(tlsConfig, 10, Duration.ofMinutes(5), Duration.ofSeconds(30)));}@Test@@ -774,7 +793,7 @@ class DockerClientProducerTest {.build();try (FlociDockerHttpClient client =- DockerClientProducer.newHttpClient(plainConfig, 10, Duration.ofSeconds(30))) {+ DockerClientProducer.newHttpClient(plainConfig, 10, Duration.ofMinutes(5), Duration.ofSeconds(30))) {assertNull(plainConfig.getSSLConfig(), "a config without TLS verify carries no SSL config");assertEquals("tcp://127.0.0.1:2375", plainConfig.getDockerHost().toString());}