Skip to content

Check for keep-alive option support #544

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 1 commit into from
Aug 8, 2025
Merged

Conversation

rschmitt
Copy link
Contributor

@rschmitt rschmitt commented Aug 8, 2025

It turns out that keep-alive options are supported on all modern combinations of Java runtimes and operating systems, with the sole excpetion of Java 8 on Windows. The keep-alive options are specifically unavailable on this combination: you can refer to them in code, but attempting to set them will result in an UnsupportedOperationException.

The good news is that there's a method we can call, Sockets.supportedOptions(), that returns a complete list of socket options supported by the current runtime. This change simply uses that functionality to check for support before setting keep-alive options on any socket.

rschmitt added a commit to rschmitt/httpcomponents-client that referenced this pull request Aug 8, 2025
It turns out that keep-alive options are supported on all modern
combinations of Java runtimes and operating systems, with the sole
excpetion of Java 8 on Windows. The keep-alive options are specifically
unavailable on this combination: you can refer to them in code, but
attempting to set them will result in an UnsupportedOperationException.

The good news is that there's a method we can call,
`Sockets.supportedOptions()`, that returns a complete list of socket
options supported by the current runtime. This change simply uses that
functionality to check for support before setting keep-alive options on
any socket.
rschmitt added a commit to rschmitt/httpcomponents-client that referenced this pull request Aug 8, 2025
@ok2c
Copy link
Member

ok2c commented Aug 8, 2025

@rschmitt Would not it be easier / neater just to catch and ignore UnsupportedOperationException?

@@ -89,4 +97,7 @@ public static int determineJRELevel() {
return 7;
}

public static boolean supportsKeepAliveOptions() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing Javadoc. Adding this here seems misleading since it's not related to reflection. Isn't there a better place?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not literally reflection, but it is introspection of the current runtime capabilities, so I figured this was a close enough match and would save me the trouble of creating a new Utils class.

@rschmitt rschmitt merged commit 076296c into apache:master Aug 8, 2025
10 checks passed
@rschmitt rschmitt deleted the check branch August 8, 2025 16:32
rschmitt added a commit to apache/httpcomponents-client that referenced this pull request Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants