Skip to content

Enable TCP keep alive for CRT HTTP client if configured #5052

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 8 commits into from
Apr 10, 2024
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/bugfix-AWSSDKforJavav2-879c193.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "eckardnet",
"description": "Set keepAile in SocketOptions to true if TcpKeepAliveConfiguration is set."
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static SocketOptions buildSocketOptions(TcpKeepAliveConfiguration tcpKeep
}

if (tcpKeepAliveConfiguration != null) {
clientSocketOptions.keepAlive = true;
clientSocketOptions.keepAliveIntervalSecs =
NumericUtils.saturatedCast(tcpKeepAliveConfiguration.keepAliveInterval().getSeconds());
clientSocketOptions.keepAliveTimeoutSecs =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@
import static software.amazon.awssdk.crt.io.TlsCipherPreference.TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05;
import static software.amazon.awssdk.crt.io.TlsCipherPreference.TLS_CIPHER_SYSTEM_DEFAULT;

import java.time.Duration;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import software.amazon.awssdk.crt.io.SocketOptions;
import software.amazon.awssdk.crt.io.TlsCipherPreference;
import software.amazon.awssdk.http.crt.TcpKeepAliveConfiguration;

class AwsCrtConfigurationUtilsTest {

Expand All @@ -51,4 +54,49 @@ private static Stream<Arguments> cipherPreferences() {
);
}

@ParameterizedTest
@MethodSource("tcpKeepAliveConfiguration")
void tcpKeepAliveConfiguration(TcpKeepAliveConfiguration tcpKeepAliveConfiguration, Duration connectionTimeout, SocketOptions expected) {
assertThat(AwsCrtConfigurationUtils.buildSocketOptions(tcpKeepAliveConfiguration, connectionTimeout))
.satisfies(socketOptions -> {
assertThat(socketOptions.connectTimeoutMs).isEqualTo(expected.connectTimeoutMs);
assertThat(socketOptions.keepAlive).isEqualTo(expected.keepAlive);
assertThat(socketOptions.keepAliveIntervalSecs).isEqualTo(expected.keepAliveIntervalSecs);
assertThat(socketOptions.keepAliveTimeoutSecs).isEqualTo(expected.keepAliveTimeoutSecs);
});
}

private static Stream<Arguments> tcpKeepAliveConfiguration() {
Duration duration1Minute = Duration.ofMinutes(1);

SocketOptions expectedConnectTimeOutOnly = new SocketOptions();
expectedConnectTimeOutOnly.connectTimeoutMs = (int) duration1Minute.toMillis();

SocketOptions expectedKeepAliveOnly = new SocketOptions();
expectedKeepAliveOnly.keepAlive = true;
expectedKeepAliveOnly.keepAliveIntervalSecs = (int)duration1Minute.getSeconds();
expectedKeepAliveOnly.keepAliveTimeoutSecs = (int)duration1Minute.getSeconds();

SocketOptions expectedAll = new SocketOptions();
expectedAll.connectTimeoutMs = (int) duration1Minute.toMillis();
expectedAll.keepAlive = true;
expectedAll.keepAliveIntervalSecs = (int)duration1Minute.getSeconds();
expectedAll.keepAliveTimeoutSecs = (int)duration1Minute.getSeconds();

return Stream.of(
Arguments.of(null, null, new SocketOptions()),
Arguments.of(null, duration1Minute, expectedConnectTimeOutOnly),
Arguments.of(
TcpKeepAliveConfiguration.builder().keepAliveInterval(duration1Minute).keepAliveTimeout(duration1Minute).build(),
null,
expectedKeepAliveOnly
),
Arguments.of(
TcpKeepAliveConfiguration.builder().keepAliveInterval(duration1Minute).keepAliveTimeout(duration1Minute).build(),
duration1Minute,
expectedAll
)
);
}

}