Skip to content

Commit 38a898b

Browse files
authored
Add TCP Keep-Alive Probe Count Configuration (#5889)
* Add Tcp Keep Alive Probe Count Configuration * Added change log * Add Tcp Keep-Alive Probe Count Configuration * Fixing coding style issues
1 parent bfd5461 commit 38a898b

File tree

4 files changed

+72
-2
lines changed

4 files changed

+72
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "AWS CRT HTTP Client",
4+
"contributor": "",
5+
"description": "Allow users to configure the number of TCP keep alive probes in the AWS CRT HTTP client through `keepAliveProbes`."
6+
}

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/TcpKeepAliveConfiguration.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ public final class TcpKeepAliveConfiguration {
2828

2929
private final Duration keepAliveInterval;
3030
private final Duration keepAliveTimeout;
31+
private final Integer keepAliveProbes;
3132

3233
private TcpKeepAliveConfiguration(DefaultTcpKeepAliveConfigurationBuilder builder) {
3334
this.keepAliveInterval = Validate.isPositive(builder.keepAliveInterval,
3435
"keepAliveInterval");
3536
this.keepAliveTimeout = Validate.isPositive(builder.keepAliveTimeout,
3637
"keepAliveTimeout");
38+
this.keepAliveProbes = builder.keepAliveProbes == null
39+
? null
40+
: Validate.isNotNegative(builder.keepAliveProbes,
41+
"keepAliveProbes");
3742
}
3843

3944
/**
@@ -50,6 +55,13 @@ public Duration keepAliveTimeout() {
5055
return keepAliveTimeout;
5156
}
5257

58+
/**
59+
* @return number of keepalive probes allowed to fail before the connection is considered lost.
60+
*/
61+
public Integer keepAliveProbes() {
62+
return keepAliveProbes;
63+
}
64+
5365
public static Builder builder() {
5466
return new DefaultTcpKeepAliveConfigurationBuilder();
5567
}
@@ -74,13 +86,22 @@ public interface Builder {
7486
*/
7587
Builder keepAliveTimeout(Duration keepAliveTimeout);
7688

89+
/**
90+
* Sets the number of keepalive probes allowed to fail before the connection is considered lost.
91+
* If not set or set to 0, OS default settings will be used for the probe count.
92+
* @param keepAliveProbes Number of keepalive probes allowed to fail before the connection is considered lost.
93+
* @return Builder
94+
*/
95+
Builder keepAliveProbes(Integer keepAliveProbes);
96+
7797
TcpKeepAliveConfiguration build();
7898
}
7999

80100
/**
81101
* An SDK-internal implementation of {@link Builder}.
82102
*/
83103
private static final class DefaultTcpKeepAliveConfigurationBuilder implements Builder {
104+
private Integer keepAliveProbes;
84105
private Duration keepAliveInterval;
85106
private Duration keepAliveTimeout;
86107

@@ -109,6 +130,12 @@ public Builder keepAliveTimeout(Duration keepAliveTimeout) {
109130
return this;
110131
}
111132

133+
@Override
134+
public Builder keepAliveProbes(Integer keepAliveProbes) {
135+
this.keepAliveProbes = keepAliveProbes;
136+
return this;
137+
}
138+
112139
@Override
113140
public TcpKeepAliveConfiguration build() {
114141
return new TcpKeepAliveConfiguration(this);

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/AwsCrtConfigurationUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public static SocketOptions buildSocketOptions(TcpKeepAliveConfiguration tcpKeep
4646
NumericUtils.saturatedCast(tcpKeepAliveConfiguration.keepAliveInterval().getSeconds());
4747
clientSocketOptions.keepAliveTimeoutSecs =
4848
NumericUtils.saturatedCast(tcpKeepAliveConfiguration.keepAliveTimeout().getSeconds());
49-
49+
if (tcpKeepAliveConfiguration.keepAliveProbes() != null) {
50+
clientSocketOptions.keepAliveMaxFailedProbes = tcpKeepAliveConfiguration.keepAliveProbes();
51+
}
5052
}
5153

5254
return clientSocketOptions;

http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/TcpKeepAliveConfigurationTest.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public void builder_allPropertiesSet() {
2929
TcpKeepAliveConfiguration.builder()
3030
.keepAliveInterval(Duration.ofMinutes(1))
3131
.keepAliveTimeout(Duration.ofSeconds(1))
32+
.keepAliveProbes(1)
3233
.build();
3334

3435
assertThat(tcpKeepAliveConfiguration.keepAliveInterval()).isEqualTo(Duration.ofMinutes(1));
3536
assertThat(tcpKeepAliveConfiguration.keepAliveTimeout()).isEqualTo(Duration.ofSeconds(1));
37+
assertThat(tcpKeepAliveConfiguration.keepAliveProbes()).isEqualTo(1);
3638
}
3739

3840
@Test
@@ -52,7 +54,29 @@ public void builder_nullKeepAliveInterval_shouldThrowException() {
5254
.build())
5355
.hasMessageContaining("keepAliveInterval");
5456
}
55-
57+
58+
@Test
59+
public void builder_nullKeepAliveMaxFailedProbes_shouldBeAllowed() {
60+
TcpKeepAliveConfiguration config = TcpKeepAliveConfiguration.builder()
61+
.keepAliveInterval(Duration.ofMinutes(1))
62+
.keepAliveTimeout(Duration.ofSeconds(1))
63+
.build();
64+
65+
assertThat(config.keepAliveProbes()).isNull();
66+
}
67+
68+
@Test
69+
public void builder_zeroKeepAliveMaxFailedProbes_shouldBeAllowed() {
70+
TcpKeepAliveConfiguration config = TcpKeepAliveConfiguration.builder()
71+
.keepAliveInterval(Duration.ofMinutes(1))
72+
.keepAliveTimeout(Duration.ofSeconds(1))
73+
.keepAliveProbes(0)
74+
.build();
75+
76+
assertThat(config.keepAliveProbes()).isEqualTo(0);
77+
}
78+
79+
5680
@Test
5781
public void builder_nonPositiveKeepAliveTimeout_shouldThrowException() {
5882
assertThatThrownBy(() ->
@@ -72,4 +96,15 @@ public void builder_nonPositiveKeepAliveInterval_shouldThrowException() {
7296
.build())
7397
.hasMessageContaining("keepAliveInterval");
7498
}
99+
100+
@Test
101+
public void builder_nonPositiveKeepAliveMaxFailedProbes_shouldThrowException() {
102+
assertThatThrownBy(() ->
103+
TcpKeepAliveConfiguration.builder()
104+
.keepAliveInterval(Duration.ofMinutes(1))
105+
.keepAliveTimeout(Duration.ofSeconds(1))
106+
.keepAliveProbes(-1)
107+
.build())
108+
.hasMessageContaining("keepAliveProbes");
109+
}
75110
}

0 commit comments

Comments
 (0)