Skip to content

Commit 40b4111

Browse files
committed
Enable new TcpKeepAlive options on Java 8
This change sets the new KeepAlive-related options directly through the JDK, instead of going through `SocketSupport`. There are two reasons for this. First, the `SocketSupport` code path doesn't work on Java 8, as it attempts to reflectively call `Socket.setOption()`, which was added in newer versions. Second, the options themselves _do_ exist on Java 8. The Java socket options were backported to Java 8 in 8u261 (July 2020): https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8194298 As for Windows, it supports `TCP_KEEPIDLE` and `TCP_KEEPINTVL` since Windows 10 version 1709 (October 2017): https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options I think it's safe at this point to simply use these features like any other socket option. Unfortunately, it doesn't look like Mojo signatures are distributed for newer maintenance releases of Java 8, so I've had to update `hc.animal-sniffer.signature.ignores`. Finally, I've added test coverage in TestTlsHandshakeTimeout to exercise this feature, as well as to assert that KeepAlive doesn't interfere with socket timeouts.
1 parent 97e4041 commit 40b4111

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestTlsHandshakeTimeout.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.hc.client5.testing.SSLTestContexts;
4343
import org.apache.hc.client5.testing.tls.TlsHandshakeTimeoutServer;
4444
import org.apache.hc.core5.http.ClassicHttpRequest;
45+
import org.apache.hc.core5.http.io.SocketConfig;
4546
import org.junit.jupiter.api.Timeout;
4647
import org.junit.jupiter.params.ParameterizedTest;
4748
import org.junit.jupiter.params.provider.ValueSource;
@@ -71,6 +72,11 @@ void testTimeout(final boolean sendServerHello) throws Exception {
7172
.setConnectTimeout(5, SECONDS)
7273
.setSocketTimeout(5, SECONDS)
7374
.build())
75+
.setDefaultSocketConfig(SocketConfig.custom()
76+
.setTcpKeepIdle(2)
77+
.setTcpKeepInterval(1)
78+
.setTcpKeepCount(5)
79+
.build())
7480
.setTlsSocketStrategy(new DefaultClientTlsStrategy(SSLTestContexts.createClientSSLContext(), HostnameVerificationPolicy.CLIENT, NoopHostnameVerifier.INSTANCE))
7581
.setDefaultTlsConfig(TlsConfig.custom()
7682
.setHandshakeTimeout(EXPECTED_TIMEOUT.toMillis(), MILLISECONDS)

httpclient5/src/main/java/org/apache/hc/client5/http/impl/io/DefaultHttpClientConnectionOperator.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
import java.util.Collections;
3737
import java.util.List;
3838

39+
import jdk.net.ExtendedSocketOptions;
40+
import jdk.net.Sockets;
3941
import org.apache.hc.client5.http.ConnectExceptionSupport;
4042
import org.apache.hc.client5.http.DnsResolver;
4143
import org.apache.hc.client5.http.SchemePortResolver;
@@ -58,7 +60,6 @@
5860
import org.apache.hc.core5.http.io.SocketConfig;
5961
import org.apache.hc.core5.http.protocol.HttpContext;
6062
import org.apache.hc.core5.io.Closer;
61-
import org.apache.hc.core5.io.SocketSupport;
6263
import org.apache.hc.core5.net.NamedEndpoint;
6364
import org.apache.hc.core5.util.Args;
6465
import org.apache.hc.core5.util.TimeValue;
@@ -318,13 +319,13 @@ private static void configureSocket(final Socket socket, final SocketConfig sock
318319
socket.setSoLinger(true, linger);
319320
}
320321
if (socketConfig.getTcpKeepIdle() > 0) {
321-
SocketSupport.setOption(socket, SocketSupport.TCP_KEEPIDLE, socketConfig.getTcpKeepIdle());
322+
Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPIDLE, socketConfig.getTcpKeepIdle());
322323
}
323324
if (socketConfig.getTcpKeepInterval() > 0) {
324-
SocketSupport.setOption(socket, SocketSupport.TCP_KEEPINTERVAL, socketConfig.getTcpKeepInterval());
325+
Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPINTERVAL, socketConfig.getTcpKeepInterval());
325326
}
326327
if (socketConfig.getTcpKeepCount() > 0) {
327-
SocketSupport.setOption(socket, SocketSupport.TCP_KEEPCOUNT, socketConfig.getTcpKeepCount());
328+
Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPCOUNT, socketConfig.getTcpKeepCount());
328329
}
329330
}
330331

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
<testcontainers.version>1.21.3</testcontainers.version>
7878
<junixsocket.version>2.10.1</junixsocket.version>
7979
<api.comparison.version>5.3</api.comparison.version>
80-
<hc.animal-sniffer.signature.ignores>javax.net.ssl.SSLEngine,javax.net.ssl.SSLParameters,java.nio.ByteBuffer,java.nio.CharBuffer</hc.animal-sniffer.signature.ignores>
80+
<hc.animal-sniffer.signature.ignores>javax.net.ssl.SSLEngine,javax.net.ssl.SSLParameters,java.nio.ByteBuffer,java.nio.CharBuffer,jdk.net.ExtendedSocketOptions,jdk.net.Sockets</hc.animal-sniffer.signature.ignores>
8181
<commons.compress.version>1.27.1</commons.compress.version>
8282
<zstd.jni.version>1.5.7-4</zstd.jni.version>
8383
</properties>

0 commit comments

Comments
 (0)