Skip to content

Commit 71dfc2c

Browse files
ajs139zoewangg
authored andcommitted
Fix to ApacheHttpClient to allow socket keepalive
1 parent 7ce36cb commit 71dfc2c

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"contributor": "ajs139",
4+
"type": "feature",
5+
"description": "Add a configuration option to enable `TCP_KEEPALIVE` on the ApacheHttpClient."
6+
}

http-client-spi/src/main/java/software/amazon/awssdk/http/SdkHttpConfigurationOption.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ public final class SdkHttpConfigurationOption<T> extends AttributeMap.Key<T> {
9090
public static final SdkHttpConfigurationOption<Boolean> REAP_IDLE_CONNECTIONS =
9191
new SdkHttpConfigurationOption<>("ReapIdleConnections", Boolean.class);
9292

93+
/**
94+
* Whether or not to use keepalive on the connection.
95+
*/
96+
public static final SdkHttpConfigurationOption<Boolean> TCP_KEEPALIVE =
97+
new SdkHttpConfigurationOption<>("TcpKeepalive", Boolean.class);
98+
9399
/**
94100
* The {@link TlsKeyManagersProvider} that will be used by the HTTP client when authenticating with a
95101
* TLS host.
@@ -120,6 +126,7 @@ public final class SdkHttpConfigurationOption<T> extends AttributeMap.Key<T> {
120126
private static final Boolean DEFAULT_REAP_IDLE_CONNECTIONS = Boolean.TRUE;
121127
private static final int DEFAULT_MAX_CONNECTIONS = 50;
122128
private static final int DEFAULT_MAX_CONNECTION_ACQUIRES = 10_000;
129+
private static final Boolean DEFAULT_TCP_KEEPALIVE = Boolean.FALSE;
123130
private static final Boolean DEFAULT_TRUST_ALL_CERTIFICATES = Boolean.FALSE;
124131

125132
private static final Protocol DEFAULT_PROTOCOL = Protocol.HTTP1_1;
@@ -140,6 +147,7 @@ public final class SdkHttpConfigurationOption<T> extends AttributeMap.Key<T> {
140147
.put(PROTOCOL, DEFAULT_PROTOCOL)
141148
.put(TRUST_ALL_CERTIFICATES, DEFAULT_TRUST_ALL_CERTIFICATES)
142149
.put(REAP_IDLE_CONNECTIONS, DEFAULT_REAP_IDLE_CONNECTIONS)
150+
.put(TCP_KEEPALIVE, DEFAULT_TCP_KEEPALIVE)
143151
.put(TLS_KEY_MANAGERS_PROVIDER, DEFAULT_TLS_KEY_MANAGERS_PROVIDER)
144152
.put(TLS_TRUST_MANAGERS_PROVIDER, DEFAULT_TLS_TRUST_MANAGERS_PROVIDER)
145153
.build();

http-clients/apache-client/src/main/java/software/amazon/awssdk/http/apache/ApacheHttpClient.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,11 @@ public interface Builder extends SdkHttpClient.Builder<ApacheHttpClient.Builder>
405405
*/
406406
Builder credentialsProvider(CredentialsProvider credentialsProvider);
407407

408+
/**
409+
* Configure the socket to use java.net.SocketOptions.SO_KEEPALIVE.
410+
*/
411+
Builder tcpKeepAlive(Boolean keepConnectionAlive);
412+
408413
/**
409414
* Configure the {@link TlsKeyManagersProvider} that will provide the {@link javax.net.ssl.KeyManager}s to use
410415
* when constructing the SSL context.
@@ -559,6 +564,16 @@ public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
559564
credentialsProvider(credentialsProvider);
560565
}
561566

567+
@Override
568+
public Builder tcpKeepAlive(Boolean keepConnectionAlive) {
569+
standardOptions.put(SdkHttpConfigurationOption.TCP_KEEPALIVE, keepConnectionAlive);
570+
return this;
571+
}
572+
573+
public void setTcpKeepAlive(Boolean keepConnectionAlive) {
574+
tcpKeepAlive(keepConnectionAlive);
575+
}
576+
562577
@Override
563578
public Builder tlsKeyManagersProvider(TlsKeyManagersProvider tlsKeyManagersProvider) {
564579
standardOptions.put(SdkHttpConfigurationOption.TLS_KEY_MANAGERS_PROVIDER, tlsKeyManagersProvider);
@@ -677,8 +692,7 @@ public X509Certificate[] getAcceptedIssuers() {
677692

678693
private SocketConfig buildSocketConfig(AttributeMap standardOptions) {
679694
return SocketConfig.custom()
680-
// TODO do we want to keep SO keep alive
681-
.setSoKeepAlive(false)
695+
.setSoKeepAlive(standardOptions.get(SdkHttpConfigurationOption.TCP_KEEPALIVE))
682696
.setSoTimeout(
683697
saturatedCast(standardOptions.get(SdkHttpConfigurationOption.READ_TIMEOUT)
684698
.toMillis()))

0 commit comments

Comments
 (0)