Skip to content

Commit

Permalink
Introduce an option for tuning SO_KEEPALIVE, close #1702
Browse files Browse the repository at this point in the history
Motivation:

We don't set SO_KEEPALIVE on the socket.

Modification:

Enable SO_KEEPALIVE by default and introduce org.asynchttpclient.soKeepAlive config option to disable it.

Result:

SO_KEEPALIVE supported
  • Loading branch information
slandelle committed Mar 4, 2020
1 parent 558d2c4 commit 0e9ad8c
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ public interface AsyncHttpClientConfig {

boolean isSoReuseAddress();

boolean isSoKeepAlive();

int getSoLinger();

int getSoSndBuf();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public class DefaultAsyncHttpClientConfig implements AsyncHttpClientConfig {
private final ByteBufAllocator allocator;
private final boolean tcpNoDelay;
private final boolean soReuseAddress;
private final boolean soKeepAlive;
private final int soLinger;
private final int soSndBuf;
private final int soRcvBuf;
Expand Down Expand Up @@ -193,6 +194,7 @@ private DefaultAsyncHttpClientConfig(// http
// tuning
boolean tcpNoDelay,
boolean soReuseAddress,
boolean soKeepAlive,
int soLinger,
int soSndBuf,
int soRcvBuf,
Expand Down Expand Up @@ -281,6 +283,7 @@ private DefaultAsyncHttpClientConfig(// http
// tuning
this.tcpNoDelay = tcpNoDelay;
this.soReuseAddress = soReuseAddress;
this.soKeepAlive = soKeepAlive;
this.soLinger = soLinger;
this.soSndBuf = soSndBuf;
this.soRcvBuf = soRcvBuf;
Expand Down Expand Up @@ -560,6 +563,11 @@ public boolean isSoReuseAddress() {
return soReuseAddress;
}

@Override
public boolean isSoKeepAlive() {
return soKeepAlive;
}

@Override
public int getSoLinger() {
return soLinger;
Expand Down Expand Up @@ -726,6 +734,7 @@ public static class Builder {
// tuning
private boolean tcpNoDelay = defaultTcpNoDelay();
private boolean soReuseAddress = defaultSoReuseAddress();
private boolean soKeepAlive = defaultSoKeepAlive();
private int soLinger = defaultSoLinger();
private int soSndBuf = defaultSoSndBuf();
private int soRcvBuf = defaultSoRcvBuf();
Expand Down Expand Up @@ -808,6 +817,7 @@ public Builder(AsyncHttpClientConfig config) {
// tuning
tcpNoDelay = config.isTcpNoDelay();
soReuseAddress = config.isSoReuseAddress();
soKeepAlive = config.isSoKeepAlive();
soLinger = config.getSoLinger();
soSndBuf = config.getSoSndBuf();
soRcvBuf = config.getSoRcvBuf();
Expand Down Expand Up @@ -1127,6 +1137,11 @@ public Builder setSoReuseAddress(boolean soReuseAddress) {
return this;
}

public Builder setSoKeepAlive(boolean soKeepAlive) {
this.soKeepAlive = soKeepAlive;
return this;
}

public Builder setSoLinger(int soLinger) {
this.soLinger = soLinger;
return this;
Expand Down Expand Up @@ -1287,6 +1302,7 @@ public DefaultAsyncHttpClientConfig build() {
cookieStore,
tcpNoDelay,
soReuseAddress,
soKeepAlive,
soLinger,
soSndBuf,
soRcvBuf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public final class AsyncHttpClientConfigDefaults {
public static final String SSL_SESSION_TIMEOUT_CONFIG = "sslSessionTimeout";
public static final String TCP_NO_DELAY_CONFIG = "tcpNoDelay";
public static final String SO_REUSE_ADDRESS_CONFIG = "soReuseAddress";
public static final String SO_KEEP_ALIVE_CONFIG = "soKeepAlive";
public static final String SO_LINGER_CONFIG = "soLinger";
public static final String SO_SND_BUF_CONFIG = "soSndBuf";
public static final String SO_RCV_BUF_CONFIG = "soRcvBuf";
Expand Down Expand Up @@ -222,6 +223,10 @@ public static boolean defaultSoReuseAddress() {
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + SO_REUSE_ADDRESS_CONFIG);
}

public static boolean defaultSoKeepAlive() {
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getBoolean(ASYNC_CLIENT_CONFIG_ROOT + SO_KEEP_ALIVE_CONFIG);
}

public static int defaultSoLinger() {
return AsyncHttpClientConfigHelper.getAsyncHttpClientConfig().getInt(ASYNC_CLIENT_CONFIG_ROOT + SO_LINGER_CONFIG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ private Bootstrap newBootstrap(ChannelFactory<? extends Channel> channelFactory,
.option(ChannelOption.ALLOCATOR, config.getAllocator() != null ? config.getAllocator() : ByteBufAllocator.DEFAULT)
.option(ChannelOption.TCP_NODELAY, config.isTcpNoDelay())
.option(ChannelOption.SO_REUSEADDR, config.isSoReuseAddress())
.option(ChannelOption.SO_KEEPALIVE, config.isSoKeepAlive())
.option(ChannelOption.AUTO_CLOSE, false);

if (config.getConnectTimeout() > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ org.asynchttpclient.sslSessionCacheSize=0
org.asynchttpclient.sslSessionTimeout=0
org.asynchttpclient.tcpNoDelay=true
org.asynchttpclient.soReuseAddress=false
org.asynchttpclient.soKeepAlive=true
org.asynchttpclient.soLinger=-1
org.asynchttpclient.soSndBuf=-1
org.asynchttpclient.soRcvBuf=-1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ public boolean isSoReuseAddress() {
return getBooleanOpt(SO_REUSE_ADDRESS_CONFIG).orElse(defaultSoReuseAddress());
}

@Override
public boolean isSoKeepAlive() {
return getBooleanOpt(SO_KEEP_ALIVE_CONFIG).orElse(defaultSoKeepAlive());
}

@Override
public int getSoLinger() {
return getIntegerOpt(SO_LINGER_CONFIG).orElse(defaultSoLinger());
Expand Down

0 comments on commit 0e9ad8c

Please sign in to comment.