Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(docker): skip stale connection checks for named pipes (#5400)

Victor Marques66756f7b9cbe
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClient.java b/src/main/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClient.java
index 7e446feccd..0b7fd8d92f 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClient.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClient.java
@@ -66,10 +66,12 @@ import java.util.stream.Stream;
* docker-java's settings (npipe and tcp hosts, TLS from the SSL config, a pool of
* {@code maxConnections} for one route, no socket read timeout on the pool, hijacked exec/attach
* upgrades through {@link HijackingHttpRequestExecutor}), validates a pooled connection idle over a
- * second before reusing it and evicts idle ones, where docker-java disabled both, and differs in two
- * more ways: a {@code unix://} host connects through {@link UnixDomainSocket}, which honours read
- * timeouts where docker-java's socket ignored them, and requests that hold a stream open get no
- * response timeout ({@link #isLongLivedStream}), while every other call keeps it.
+ * second before reuse on transports that honour socket read timeouts, and evicts idle ones, where
+ * docker-java disabled both. A {@code unix://} host connects through {@link UnixDomainSocket},
+ * which honours read timeouts where docker-java's socket ignored them. Named pipes skip stale
+ * validation because docker-java's {@link NamedPipeSocket} read can block despite the 1 ms timeout
+ * used by httpclient5's validation check. Requests that hold a stream open get no response timeout
+ * ({@link #isLongLivedStream}), while every other call keeps it.
*/
public final class FlociDockerHttpClient implements DockerHttpClient {
@@ -121,11 +123,12 @@ public final class FlociDockerHttpClient implements DockerHttpClient {
.build());
connectionManager.setMaxTotal(maxConnections);
connectionManager.setDefaultMaxPerRoute(maxConnections);
- connectionManager.setDefaultConnectionConfig(ConnectionConfig.custom()
- .setValidateAfterInactivity(VALIDATE_AFTER_INACTIVITY)
+ ConnectionConfig connectionConfig = ConnectionConfig.custom()
+ .setValidateAfterInactivity(staleConnectionValidationFor(dockerHost))
.setConnectTimeout(connectionTimeout != null
? Timeout.of(connectionTimeout.toNanos(), TimeUnit.NANOSECONDS) : null)
- .build());
+ .build();
+ connectionManager.setDefaultConnectionConfig(connectionConfig);
RequestConfig requestConfig = RequestConfig.custom()
.setResponseTimeout(responseTimeout != null
@@ -172,6 +175,12 @@ public final class FlociDockerHttpClient implements DockerHttpClient {
name -> "https".equalsIgnoreCase(name) ? tlsSocketStrategy : null);
}
+ /** Selects stale validation only when the socket honours its short read timeout. */
+ static TimeValue staleConnectionValidationFor(URI dockerHost) {
+ return "npipe".equalsIgnoreCase(dockerHost.getScheme())
+ ? TimeValue.NEG_ONE_MILLISECOND : VALIDATE_AFTER_INACTIVITY;
+ }
+
@Override
public Response execute(Request request) {
HttpContext context = new HttpCoreContext();
diff --git a/src/test/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClientTest.java b/src/test/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClientTest.java
index e4d7efc3bf..a65194bb34 100644
--- a/src/test/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClientTest.java
+++ b/src/test/java/io/github/hectorvent/floci/core/common/docker/FlociDockerHttpClientTest.java
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.core.common.docker;
import com.github.dockerjava.transport.DockerHttpClient;
import org.apache.hc.core5.http.ConnectionRequestTimeoutException;
+import org.apache.hc.core5.util.TimeValue;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.io.TempDir;
@@ -34,6 +35,16 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class FlociDockerHttpClientTest {
+ @Test
+ void staleConnectionValidationOnlyUsesSocketsThatHonorReadTimeouts() {
+ assertEquals(TimeValue.ofSeconds(1), FlociDockerHttpClient.staleConnectionValidationFor(
+ URI.create("unix:///var/run/docker.sock")));
+ assertEquals(TimeValue.ofSeconds(1), FlociDockerHttpClient.staleConnectionValidationFor(
+ URI.create("tcp://127.0.0.1:2375")));
+ assertEquals(TimeValue.NEG_ONE_MILLISECOND, FlociDockerHttpClient.staleConnectionValidationFor(
+ URI.create("npipe:////./pipe/docker_engine")));
+ }
+
// Catches: a full pool making a call wait out httpclient5's three-minute default instead of
// failing after the configured lease timeout.
@Test