| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -150,6 +150,9 @@ public abstract class AbstractDockerCmdExecFactory implements DockerCmdExecFacto | |||
| 150 | 150 | ||
| 151 | 151 | private DockerClientConfig dockerClientConfig; | |
| 152 | 152 | ||
| 153 | + protected Integer connectTimeout; | ||
| 154 | + protected Integer readTimeout; | ||
| 155 | + | ||
| 153 | 156 | protected DockerClientConfig getDockerClientConfig() { | |
| 154 | 157 | checkNotNull(dockerClientConfig, | |
| 155 | 158 | "Factor not initialized, dockerClientConfig not set. You probably forgot to call init()!"); | |
@@ -172,6 +175,22 @@ public CopyArchiveToContainerCmd.Exec createCopyArchiveToContainerCmdExec() { | |||
| 172 | 175 | return new CopyArchiveToContainerCmdExec(getBaseResource(), getDockerClientConfig()); | |
| 173 | 176 | } | |
| 174 | 177 | ||
| 178 | + /** | ||
| 179 | + * Configure connection timeout in milliseconds | ||
| 180 | + */ | ||
| 181 | + public AbstractDockerCmdExecFactory withConnectTimeout(Integer connectTimeout) { | ||
| 182 | + this.connectTimeout = connectTimeout; | ||
| 183 | + return this; | ||
| 184 | + } | ||
| 185 | + | ||
| 186 | + /** | ||
| 187 | + * Configure read timeout in milliseconds | ||
| 188 | + */ | ||
| 189 | + public AbstractDockerCmdExecFactory withReadTimeout(Integer readTimeout) { | ||
| 190 | + this.readTimeout = readTimeout; | ||
| 191 | + return this; | ||
| 192 | + } | ||
| 193 | + | ||
| 175 | 194 | @Override | |
| 176 | 195 | public AuthCmd.Exec createAuthCmdExec() { | |
| 177 | 196 | return new AuthCmdExec(getBaseResource(), getDockerClientConfig()); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,10 +50,6 @@ public class JerseyDockerCmdExecFactory extends AbstractDockerCmdExecFactory { | |||
| 50 | 50 | ||
| 51 | 51 | private JerseyWebTarget baseResource; | |
| 52 | 52 | ||
| 53 | - private Integer readTimeout = null; | ||
| 54 | - | ||
| 55 | - private Integer connectTimeout = null; | ||
| 56 | - | ||
| 57 | 53 | private Integer maxTotalConnections = null; | |
| 58 | 54 | ||
| 59 | 55 | private Integer maxPerRouteConnections = null; | |
@@ -262,16 +258,6 @@ public void close() throws IOException { | |||
| 262 | 258 | connManager.close(); | |
| 263 | 259 | } | |
| 264 | 260 | ||
| 265 | - public JerseyDockerCmdExecFactory withReadTimeout(Integer readTimeout) { | ||
| 266 | - this.readTimeout = readTimeout; | ||
| 267 | - return this; | ||
| 268 | - } | ||
| 269 | - | ||
| 270 | - public JerseyDockerCmdExecFactory withConnectTimeout(Integer connectTimeout) { | ||
| 271 | - this.connectTimeout = connectTimeout; | ||
| 272 | - return this; | ||
| 273 | - } | ||
| 274 | - | ||
| 275 | 261 | public JerseyDockerCmdExecFactory withMaxTotalConnections(Integer maxTotalConnections) { | |
| 276 | 262 | this.maxTotalConnections = maxTotalConnections; | |
| 277 | 263 | return this; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | package com.github.dockerjava.netty; | |
| 2 | 2 | ||
| 3 | 3 | import static com.google.common.base.Preconditions.checkNotNull; | |
| 4 | + import static java.util.Objects.nonNull; | ||
| 4 | 5 | ||
| 5 | 6 | import java.io.IOException; | |
| 6 | 7 | import java.net.InetAddress; | |
@@ -58,7 +59,7 @@ | |||
| 58 | 59 | * @see https://docs.docker.com/engine/reference/api/docker_remote_api_v1.21/#attach-to-a-container | |
| 59 | 60 | * @see https://docs.docker.com/engine/reference/api/docker_remote_api_v1.21/#exec-start | |
| 60 | 61 | */ | |
| 61 | - public class NettyDockerCmdExecFactory extends AbstractDockerCmdExecFactory implements DockerCmdExecFactory { | ||
| 62 | + public class NettyDockerCmdExecFactory extends AbstractDockerCmdExecFactory { | ||
| 62 | 63 | ||
| 63 | 64 | private static String threadPrefix = "dockerjava-netty"; | |
| 64 | 65 | ||
@@ -88,10 +89,6 @@ public DuplexChannel getChannel() { | |||
| 88 | 89 | } | |
| 89 | 90 | }; | |
| 90 | 91 | ||
| 91 | - private Integer connectTimeout = null; | ||
| 92 | - | ||
| 93 | - private Integer readTimeout = null; | ||
| 94 | - | ||
| 95 | 92 | @Override | |
| 96 | 93 | public void init(DockerClientConfig dockerClientConfig) { | |
| 97 | 94 | super.init(dockerClientConfig); | |
@@ -292,29 +289,13 @@ public void close() throws IOException { | |||
| 292 | 289 | eventLoopGroup.shutdownGracefully(); | |
| 293 | 290 | } | |
| 294 | 291 | ||
| 295 | - /** | ||
| 296 | - * Configure connection timeout in milliseconds | ||
| 297 | - */ | ||
| 298 | - public NettyDockerCmdExecFactory withConnectTimeout(Integer connectTimeout) { | ||
| 299 | - this.connectTimeout = connectTimeout; | ||
| 300 | - return this; | ||
| 301 | - } | ||
| 302 | - | ||
| 303 | - /** | ||
| 304 | - * Configure read timeout in milliseconds | ||
| 305 | - */ | ||
| 306 | - public NettyDockerCmdExecFactory withReadTimeout(Integer readTimeout) { | ||
| 307 | - this.readTimeout = readTimeout; | ||
| 308 | - return this; | ||
| 309 | - } | ||
| 310 | - | ||
| 311 | 292 | private <T extends Channel> T configure(T channel) { | |
| 312 | 293 | ChannelConfig channelConfig = channel.config(); | |
| 313 | 294 | ||
| 314 | - if (connectTimeout != null) { | ||
| 295 | + if (nonNull(connectTimeout)) { | ||
| 315 | 296 | channelConfig.setConnectTimeoutMillis(connectTimeout); | |
| 316 | 297 | } | |
| 317 | - if (readTimeout != null) { | ||
| 298 | + if (nonNull(readTimeout)) { | ||
| 318 | 299 | channel.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler()); | |
| 319 | 300 | } | |
| 320 | 301 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,23 +20,45 @@ | |||
| 20 | 20 | import java.util.Collections; | |
| 21 | 21 | import java.util.concurrent.TimeUnit; | |
| 22 | 22 | ||
| 23 | + import static java.util.Objects.nonNull; | ||
| 24 | + | ||
| 23 | 25 | public class OkHttpDockerCmdExecFactory extends AbstractDockerCmdExecFactory { | |
| 24 | 26 | ||
| 25 | 27 | private static final String SOCKET_SUFFIX = ".socket"; | |
| 26 | 28 | ||
| 27 | 29 | private ObjectMapper objectMapper; | |
| 28 | 30 | ||
| 29 | 31 | private OkHttpClient okHttpClient; | |
| 32 | + private Boolean retryOnConnectionFailure; | ||
| 30 | 33 | ||
| 31 | 34 | private HttpUrl baseUrl; | |
| 32 | 35 | ||
| 36 | + public OkHttpDockerCmdExecFactory setRetryOnConnectionFailure(Boolean retryOnConnectionFailure) { | ||
| 37 | + this.retryOnConnectionFailure = retryOnConnectionFailure; | ||
| 38 | + return this; | ||
| 39 | + } | ||
| 40 | + | ||
| 33 | 41 | @Override | |
| 34 | 42 | public void init(DockerClientConfig dockerClientConfig) { | |
| 35 | 43 | super.init(dockerClientConfig); | |
| 36 | 44 | ||
| 37 | - OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder() | ||
| 38 | - .readTimeout(0, TimeUnit.SECONDS) | ||
| 39 | - .retryOnConnectionFailure(true); | ||
| 45 | + OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder(); | ||
| 46 | + if (nonNull(readTimeout)) { | ||
| 47 | + clientBuilder.readTimeout(readTimeout, TimeUnit.MILLISECONDS); | ||
| 48 | + } else { | ||
| 49 | + // default is too small for most docker commands, set default like in jersey/netty | ||
| 50 | + clientBuilder.readTimeout(0, TimeUnit.MILLISECONDS); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + if (nonNull(connectTimeout)) { | ||
| 54 | + clientBuilder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + if (nonNull(retryOnConnectionFailure)) { | ||
| 58 | + clientBuilder.retryOnConnectionFailure(retryOnConnectionFailure); | ||
| 59 | + } else { | ||
| 60 | + clientBuilder.retryOnConnectionFailure(true); | ||
| 61 | + } | ||
| 40 | 62 | ||
| 41 | 63 | URI dockerHost = dockerClientConfig.getDockerHost(); | |
| 42 | 64 | switch (dockerHost.getScheme()) { | |
@@ -45,11 +67,9 @@ public void init(DockerClientConfig dockerClientConfig) { | |||
| 45 | 67 | String socketPath = dockerHost.getPath(); | |
| 46 | 68 | ||
| 47 | 69 | if ("unix".equals(dockerHost.getScheme())) { | |
| 48 | - clientBuilder | ||
| 49 | - .socketFactory(new UnixSocketFactory(socketPath)); | ||
| 70 | + clientBuilder.socketFactory(new UnixSocketFactory(socketPath)); | ||
| 50 | 71 | } else { | |
| 51 | - clientBuilder | ||
| 52 | - .socketFactory(new NamedPipeSocketFactory(socketPath)); | ||
| 72 | + clientBuilder.socketFactory(new NamedPipeSocketFactory(socketPath)); | ||
| 53 | 73 | } | |
| 54 | 74 | ||
| 55 | 75 | clientBuilder | |
@@ -72,8 +92,7 @@ public void init(DockerClientConfig dockerClientConfig) { | |||
| 72 | 92 | SSLContext sslContext = sslConfig.getSSLContext(); | |
| 73 | 93 | if (sslContext != null) { | |
| 74 | 94 | isSSL = true; | |
| 75 | - clientBuilder | ||
| 76 | - .sslSocketFactory(sslContext.getSocketFactory(), new TrustAllX509TrustManager()); | ||
| 95 | + clientBuilder.sslSocketFactory(sslContext.getSocketFactory(), new TrustAllX509TrustManager()); | ||
| 77 | 96 | } | |
| 78 | 97 | } catch (Exception e) { | |
| 79 | 98 | throw new RuntimeException(e); | |
@@ -88,14 +107,14 @@ public void init(DockerClientConfig dockerClientConfig) { | |||
| 88 | 107 | case "unix": | |
| 89 | 108 | case "npipe": | |
| 90 | 109 | baseUrlBuilder = new HttpUrl.Builder() | |
| 91 | - .scheme("http") | ||
| 92 | - .host("docker" + SOCKET_SUFFIX); | ||
| 110 | + .scheme("http") | ||
| 111 | + .host("docker" + SOCKET_SUFFIX); | ||
| 93 | 112 | break; | |
| 94 | 113 | case "tcp": | |
| 95 | 114 | baseUrlBuilder = new HttpUrl.Builder() | |
| 96 | - .scheme(isSSL ? "https" : "http") | ||
| 97 | - .host(dockerHost.getHost()) | ||
| 98 | - .port(dockerHost.getPort()); | ||
| 115 | + .scheme(isSSL ? "https" : "http") | ||
| 116 | + .host(dockerHost.getHost()) | ||
| 117 | + .port(dockerHost.getPort()); | ||
| 99 | 118 | break; | |
| 100 | 119 | default: | |
| 101 | 120 | baseUrlBuilder = HttpUrl.get(dockerHost.toString()).newBuilder(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -23,19 +23,19 @@ public enum FactoryType { | |||
| 23 | 23 | NETTY(true) { | |
| 24 | 24 | @Override | |
| 25 | 25 | public DockerCmdExecFactory createExecFactory() { | |
| 26 | - return new NettyDockerCmdExecFactory().withConnectTimeout(10 * 1000); | ||
| 26 | + return new NettyDockerCmdExecFactory().withConnectTimeout(30 * 1000); | ||
| 27 | 27 | } | |
| 28 | 28 | }, | |
| 29 | 29 | JERSEY(false) { | |
| 30 | 30 | @Override | |
| 31 | 31 | public DockerCmdExecFactory createExecFactory() { | |
| 32 | - return new JerseyDockerCmdExecFactory().withConnectTimeout(10 * 1000); | ||
| 32 | + return new JerseyDockerCmdExecFactory().withConnectTimeout(30 * 1000); | ||
| 33 | 33 | } | |
| 34 | 34 | }, | |
| 35 | 35 | OKHTTP(true) { | |
| 36 | 36 | @Override | |
| 37 | 37 | public DockerCmdExecFactory createExecFactory() { | |
| 38 | - return new OkHttpDockerCmdExecFactory(); | ||
| 38 | + return new OkHttpDockerCmdExecFactory().withConnectTimeout(30 * 1000); | ||
| 39 | 39 | } | |
| 40 | 40 | }; | |
| 41 | 41 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,8 @@ | |||
| 1 | 1 | package com.github.dockerjava.cmd; | |
| 2 | 2 | ||
| 3 | + import com.github.dockerjava.api.async.ResultCallbackTemplate; | ||
| 3 | 4 | import com.github.dockerjava.api.command.CreateContainerResponse; | |
| 4 | 5 | import com.github.dockerjava.api.model.Statistics; | |
| 5 | - import com.github.dockerjava.core.async.ResultCallbackTemplate; | ||
| 6 | 6 | import org.junit.Test; | |
| 7 | 7 | import org.slf4j.Logger; | |
| 8 | 8 | import org.slf4j.LoggerFactory; | |
@@ -28,8 +28,10 @@ public void testStatsStreaming() throws InterruptedException, IOException { | |||
| 28 | 28 | dockerRule.getClient().startContainerCmd(container.getId()).exec(); | |
| 29 | 29 | ||
| 30 | 30 | boolean gotStats = false; | |
| 31 | - try (StatsCallbackTest statsCallback = dockerRule.getClient().statsCmd(container.getId()).exec( | ||
| 32 | - new StatsCallbackTest(countDownLatch))) { | ||
| 31 | + try (StatsCallbackTest statsCallback = dockerRule.getClient() | ||
| 32 | + .statsCmd(container.getId()) | ||
| 33 | + .exec(new StatsCallbackTest(countDownLatch))) { | ||
| 34 | + | ||
| 33 | 35 | assertTrue(countDownLatch.await(10, TimeUnit.SECONDS)); | |
| 34 | 36 | gotStats = statsCallback.gotStats(); | |
| 35 | 37 | ||
@@ -52,8 +54,9 @@ public void testStatsNoStreaming() throws InterruptedException, IOException { | |||
| 52 | 54 | ||
| 53 | 55 | dockerRule.getClient().startContainerCmd(container.getId()).exec(); | |
| 54 | 56 | ||
| 55 | - try (StatsCallbackTest statsCallback = dockerRule.getClient().statsCmd(container.getId()).withNoStream(true).exec( | ||
| 56 | - new StatsCallbackTest(countDownLatch))) { | ||
| 57 | + try (StatsCallbackTest statsCallback = dockerRule.getClient().statsCmd(container.getId()) | ||
| 58 | + .withNoStream(true) | ||
| 59 | + .exec(new StatsCallbackTest(countDownLatch))) { | ||
| 57 | 60 | countDownLatch.await(5, TimeUnit.SECONDS); | |
| 58 | 61 | ||
| 59 | 62 | LOG.info("Stop stats collection"); | |
@@ -67,7 +70,7 @@ public void testStatsNoStreaming() throws InterruptedException, IOException { | |||
| 67 | 70 | assertEquals("Expected stats called only once", countDownLatch.getCount(), NUM_STATS - 1); | |
| 68 | 71 | } | |
| 69 | 72 | ||
| 70 | - private class StatsCallbackTest extends ResultCallbackTemplate<StatsCallbackTest, Statistics> { | ||
| 73 | + private static class StatsCallbackTest extends ResultCallbackTemplate<StatsCallbackTest, Statistics> { | ||
| 71 | 74 | private final CountDownLatch countDownLatch; | |
| 72 | 75 | ||
| 73 | 76 | private Boolean gotStats = false; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments