Skip to content

Fix ApacheHttpClient to use socket keep alive when appropriate #2373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-31396bc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "ajs139",
"type": "feature",
"description": "Add a configuration option to enable `TCP_KEEPALIVE` on the ApacheHttpClient."
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ public final class SdkHttpConfigurationOption<T> extends AttributeMap.Key<T> {
public static final SdkHttpConfigurationOption<Boolean> REAP_IDLE_CONNECTIONS =
new SdkHttpConfigurationOption<>("ReapIdleConnections", Boolean.class);

/**
* Whether or not to use keepalive on the connection.
*/
public static final SdkHttpConfigurationOption<Boolean> TCP_KEEPALIVE =
new SdkHttpConfigurationOption<>("TcpKeepalive", Boolean.class);

/**
* The {@link TlsKeyManagersProvider} that will be used by the HTTP client when authenticating with a
* TLS host.
Expand Down Expand Up @@ -120,6 +126,7 @@ public final class SdkHttpConfigurationOption<T> extends AttributeMap.Key<T> {
private static final Boolean DEFAULT_REAP_IDLE_CONNECTIONS = Boolean.TRUE;
private static final int DEFAULT_MAX_CONNECTIONS = 50;
private static final int DEFAULT_MAX_CONNECTION_ACQUIRES = 10_000;
private static final Boolean DEFAULT_TCP_KEEPALIVE = Boolean.FALSE;
private static final Boolean DEFAULT_TRUST_ALL_CERTIFICATES = Boolean.FALSE;

private static final Protocol DEFAULT_PROTOCOL = Protocol.HTTP1_1;
Expand All @@ -140,6 +147,7 @@ public final class SdkHttpConfigurationOption<T> extends AttributeMap.Key<T> {
.put(PROTOCOL, DEFAULT_PROTOCOL)
.put(TRUST_ALL_CERTIFICATES, DEFAULT_TRUST_ALL_CERTIFICATES)
.put(REAP_IDLE_CONNECTIONS, DEFAULT_REAP_IDLE_CONNECTIONS)
.put(TCP_KEEPALIVE, DEFAULT_TCP_KEEPALIVE)
.put(TLS_KEY_MANAGERS_PROVIDER, DEFAULT_TLS_KEY_MANAGERS_PROVIDER)
.put(TLS_TRUST_MANAGERS_PROVIDER, DEFAULT_TLS_TRUST_MANAGERS_PROVIDER)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ public interface Builder extends SdkHttpClient.Builder<ApacheHttpClient.Builder>
*/
Builder credentialsProvider(CredentialsProvider credentialsProvider);

/**
* Configure the socket to use java.net.SocketOptions.SO_KEEPALIVE.
*/
Builder tcpKeepAlive(Boolean keepConnectionAlive);

/**
* Configure the {@link TlsKeyManagersProvider} that will provide the {@link javax.net.ssl.KeyManager}s to use
* when constructing the SSL context.
Expand Down Expand Up @@ -559,6 +564,16 @@ public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
credentialsProvider(credentialsProvider);
}

@Override
public Builder tcpKeepAlive(Boolean keepConnectionAlive) {
standardOptions.put(SdkHttpConfigurationOption.TCP_KEEPALIVE, keepConnectionAlive);
return this;
}

public void setTcpKeepAlive(Boolean keepConnectionAlive) {
tcpKeepAlive(keepConnectionAlive);
}

@Override
public Builder tlsKeyManagersProvider(TlsKeyManagersProvider tlsKeyManagersProvider) {
standardOptions.put(SdkHttpConfigurationOption.TLS_KEY_MANAGERS_PROVIDER, tlsKeyManagersProvider);
Expand Down Expand Up @@ -677,8 +692,7 @@ public X509Certificate[] getAcceptedIssuers() {

private SocketConfig buildSocketConfig(AttributeMap standardOptions) {
return SocketConfig.custom()
// TODO do we want to keep SO keep alive
.setSoKeepAlive(false)
.setSoKeepAlive(standardOptions.get(SdkHttpConfigurationOption.TCP_KEEPALIVE))
.setSoTimeout(
saturatedCast(standardOptions.get(SdkHttpConfigurationOption.READ_TIMEOUT)
.toMillis()))
Expand Down