Skip to content

Commit

Permalink
Merge pull request #260 from xjdr/dc/TACO-326-supreme
Browse files Browse the repository at this point in the history
TACO-326-supreme removing the idle channel handler from the ClientCha…
  • Loading branch information
khappucino committed Jun 8, 2018
2 parents 6baf995 + a69cac0 commit 9977eab
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 5 deletions.
13 changes: 13 additions & 0 deletions xio-core/src/main/java/com/xjeffrose/xio/client/ClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
@Accessors(fluent = true)
@Getter
public class ClientConfig {

private final Map<ChannelOption<Object>, Object> bootstrapOptions;
private final String name;
private final TlsConfig tls;
private final boolean messageLoggerEnabled;
private final InetSocketAddress local;
private final InetSocketAddress remote;
private final IdleTimeoutConfig idleTimeoutConfig;

public String getName() {
return name;
Expand Down Expand Up @@ -48,12 +50,23 @@ public ClientConfig(Config config) {
local = new InetSocketAddress(config.getString("localIp"), config.getInt("localPort"));
}
remote = new InetSocketAddress(config.getString("remoteIp"), config.getInt("remotePort"));

boolean idleTimeoutEnabled = config.getBoolean("idleTimeoutEnabled");
int idleTimeoutDuration = 0;
if (idleTimeoutEnabled) {
idleTimeoutDuration = config.getInt("idleTimeoutDuration");
}
idleTimeoutConfig = new IdleTimeoutConfig(idleTimeoutEnabled, idleTimeoutDuration);
}

public boolean isTlsEnabled() {
return tls.isUseSsl();
}

public IdleTimeoutConfig getIdleTimeoutConfig() {
return idleTimeoutConfig;
};

public static ClientConfig fromConfig(String key, Config config) {
return new ClientConfig(config.getConfig(key));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.xjeffrose.xio.client;

public class IdleTimeoutConfig {
public final boolean enabled;
public final int duration;

public IdleTimeoutConfig(boolean enabled, int duration) {
this.enabled = enabled;
this.duration = duration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,16 @@ protected void initChannel(Channel channel) throws Exception {
val traceHandler = tracing.newClientHandler(state.config.isTlsEnabled());
Pipelines.addHandler(channel.pipeline(), "distributed tracing", traceHandler);
}

if (state.idleTimeoutEnabled) {
int duration = state.idleTimeoutDuration;
channel
.pipeline()
.addLast("idle handler", new XioIdleDisconnectHandler(duration, duration, duration));
}

channel
.pipeline()
.addLast("idle handler", new XioIdleDisconnectHandler(60, 60, 60))
.addLast("message logging", new XioMessageLogger(Client.class, "objects"))
.addLast("request buffer", new RequestBuffer())
.addLast(APP_HANDLER, appHandler.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class ClientState {
public final ClientConfig config;
public final InetSocketAddress remote;
public final SslContext sslContext;
public final boolean idleTimeoutEnabled;
public final int idleTimeoutDuration;

private static SslContext sslContext(boolean enableTls, ClientConfig clientConfig) {
if (enableTls) {
Expand All @@ -31,6 +33,8 @@ public ClientState(
this.config = config;
this.remote = remote;
this.sslContext = sslContext;
idleTimeoutEnabled = config.getIdleTimeoutConfig().enabled;
idleTimeoutDuration = config.getIdleTimeoutConfig().duration;
}

public ClientState(
Expand All @@ -42,9 +46,6 @@ public ClientState(
}

public ClientState(ClientChannelConfiguration channelConfig, ClientConfig config) {
this.channelConfig = channelConfig;
this.config = config;
this.remote = config.remote();
this.sslContext = sslContext(config.isTlsEnabled(), config);
this(channelConfig, config, config.remote(), sslContext(config.isTlsEnabled(), config));
}
}
2 changes: 2 additions & 0 deletions xio-core/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ xio {
remotePort = 0
localIp = ""
localPort = 0
idleTimeoutEnabled = false
idleTimeoutDuration = 0
settings {
messageLoggerEnabled = true
tls {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.typesafe.config.ConfigFactory;
import com.xjeffrose.xio.bootstrap.ChannelConfiguration;
import com.xjeffrose.xio.client.ClientConfig;
import com.xjeffrose.xio.core.XioIdleDisconnectHandler;
import com.xjeffrose.xio.tracing.HttpClientTracingHandler;
import com.xjeffrose.xio.tracing.XioTracing;
import io.netty.channel.ChannelHandler;
Expand Down Expand Up @@ -111,4 +112,36 @@ public void testDisableSsl() {
val result = testChannel.pipeline().get(SslHandler.class);
assertNull(result);
}

@Test
public void testDisableIdleTimeout() {
val channelConfig = ChannelConfiguration.clientConfig(1, "worker");
val clientConfig = new ClientConfig(ConfigFactory.load().getConfig("xio.idleDisabledClient"));
val clientState = new ClientState(channelConfig, clientConfig);
// when we have enabled Tracing the tracing returns a non-null newClientHandler
when(tracing.newClientHandler(clientConfig.getTls().isUseSsl())).thenReturn(tracingHandler);

subject = new ClientChannelInitializer(clientState, () -> appHandler, tracing);

// Assert that we did not add a HttpClientTracingHandler to the pipeline
val testChannel = new EmbeddedChannel(subject);
val result = testChannel.pipeline().get(XioIdleDisconnectHandler.class);
assertNull(result);
}

@Test
public void testEnableIdleTimeout() {
val channelConfig = ChannelConfiguration.clientConfig(1, "worker");
val clientConfig = new ClientConfig(ConfigFactory.load().getConfig("xio.idleEnabledClient"));
val clientState = new ClientState(channelConfig, clientConfig);
// when we have enabled Tracing the tracing returns a non-null newClientHandler
when(tracing.newClientHandler(clientConfig.getTls().isUseSsl())).thenReturn(tracingHandler);

subject = new ClientChannelInitializer(clientState, () -> appHandler, tracing);

// Assert that we did not add a HttpClientTracingHandler to the pipeline
val testChannel = new EmbeddedChannel(subject);
val result = testChannel.pipeline().get(XioIdleDisconnectHandler.class);
assertNotNull(result);
}
}
14 changes: 14 additions & 0 deletions xio-core/src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ xio {
}
}
}

sslClient = ${xio.clientTemplate} {
name = sslClient
settings {
Expand All @@ -345,6 +346,19 @@ xio {
}
}
}

idleDisabledClient = ${xio.clientTemplate} {
name = basicClient
idleTimeoutEnabled = false
idleTimeoutDuration = 0
}

idleEnabledClient = ${xio.clientTemplate} {
name = basicClient
idleTimeoutEnabled = true
idleTimeoutDuration = 60
}

invalidZipkinParameters = ${xio.clientTemplate} {
name = invalidZipkinParameters
settings {
Expand Down

0 comments on commit 9977eab

Please sign in to comment.