Skip to content

Commit 75e9a2c

Browse files
authored
Expose netty connect timeout (#725)
1 parent 5736b4b commit 75e9a2c

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/main/java/com/github/dockerjava/netty/NettyDockerCmdExecFactory.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@
108108
import com.github.dockerjava.netty.exec.RenameContainerCmdExec;
109109

110110
import io.netty.bootstrap.Bootstrap;
111+
import io.netty.channel.Channel;
112+
import io.netty.channel.ChannelConfig;
113+
import io.netty.channel.ChannelFactory;
111114
import io.netty.channel.ChannelInitializer;
112115
import io.netty.channel.EventLoopGroup;
113116
import io.netty.channel.epoll.EpollDomainSocketChannel;
@@ -178,6 +181,8 @@ public DuplexChannel getChannel() {
178181
}
179182
};
180183

184+
private Integer connectTimeout = null;
185+
181186
@Override
182187
public void init(DockerClientConfig dockerClientConfig) {
183188
checkNotNull(dockerClientConfig, "config was not specified");
@@ -218,7 +223,15 @@ private class UnixDomainSocketInitializer implements NettyInitializer {
218223
@Override
219224
public EventLoopGroup init(Bootstrap bootstrap, DockerClientConfig dockerClientConfig) {
220225
EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
221-
bootstrap.group(epollEventLoopGroup).channel(EpollDomainSocketChannel.class)
226+
227+
ChannelFactory<EpollDomainSocketChannel> factory = new ChannelFactory<EpollDomainSocketChannel>() {
228+
@Override
229+
public EpollDomainSocketChannel newChannel() {
230+
return configure(new EpollDomainSocketChannel());
231+
}
232+
};
233+
234+
bootstrap.group(epollEventLoopGroup).channelFactory(factory)
222235
.handler(new ChannelInitializer<UnixChannel>() {
223236
@Override
224237
protected void initChannel(final UnixChannel channel) throws Exception {
@@ -245,7 +258,14 @@ public EventLoopGroup init(Bootstrap bootstrap, final DockerClientConfig dockerC
245258

246259
Security.addProvider(new BouncyCastleProvider());
247260

248-
bootstrap.group(nioEventLoopGroup).channel(NioSocketChannel.class)
261+
ChannelFactory<NioSocketChannel> factory = new ChannelFactory<NioSocketChannel>() {
262+
@Override
263+
public NioSocketChannel newChannel() {
264+
return configure(new NioSocketChannel());
265+
}
266+
};
267+
268+
bootstrap.group(nioEventLoopGroup).channelFactory(factory)
249269
.handler(new ChannelInitializer<SocketChannel>() {
250270
@Override
251271
protected void initChannel(final SocketChannel channel) throws Exception {
@@ -580,6 +600,24 @@ public void close() throws IOException {
580600
eventLoopGroup.shutdownGracefully();
581601
}
582602

603+
/**
604+
* Configure connection timeout in milliseconds
605+
*/
606+
public NettyDockerCmdExecFactory withConnectTimeout(Integer connectTimeout) {
607+
this.connectTimeout = connectTimeout;
608+
return this;
609+
}
610+
611+
private <T extends Channel> T configure(T channel) {
612+
ChannelConfig channelConfig = channel.config();
613+
614+
if (connectTimeout != null) {
615+
channelConfig.setConnectTimeoutMillis(connectTimeout);
616+
}
617+
618+
return channel;
619+
}
620+
583621
private WebTarget getBaseResource() {
584622
return new WebTarget(channelProvider);
585623
}

0 commit comments

Comments
 (0)