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
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
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
import org.apache.hc.core5.pool.PoolEntry;
import org.apache.hc.core5.pool.PoolStats;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.ReflectionUtils;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

Expand Down Expand Up @@ -240,6 +241,7 @@ public <T> T execute(
}
}

@SuppressWarnings("Since15")
private HttpClientConnection createConnection(final Socket sock, final HttpHost targetHost) throws IOException {
sock.setSoTimeout(socketConfig.getSoTimeout().toMillisecondsIntBound());
sock.setReuseAddress(socketConfig.isSoReuseAddress());
Expand All @@ -251,14 +253,16 @@ private HttpClientConnection createConnection(final Socket sock, final HttpHost
if (socketConfig.getSndBufSize() > 0) {
sock.setSendBufferSize(socketConfig.getSndBufSize());
}
if (this.socketConfig.getTcpKeepIdle() > 0) {
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
}
if (this.socketConfig.getTcpKeepInterval() > 0) {
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
}
if (this.socketConfig.getTcpKeepCount() > 0) {
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
if (ReflectionUtils.supportsKeepAliveOptions()) {
if (this.socketConfig.getTcpKeepIdle() > 0) {
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
}
if (this.socketConfig.getTcpKeepInterval() > 0) {
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
}
if (this.socketConfig.getTcpKeepCount() > 0) {
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
}
}
final int linger = socketConfig.getSoLinger().toMillisecondsIntBound();
if (linger >= 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.hc.core5.io.Closer;
import org.apache.hc.core5.io.ModalCloseable;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.ReflectionUtils;
import org.apache.hc.core5.util.TimeValue;
import org.apache.hc.core5.util.Timeout;

Expand Down Expand Up @@ -139,6 +140,7 @@ public int getLocalPort() {
return -1;
}

@SuppressWarnings("Since15")
public void start() throws IOException {
if (this.status.compareAndSet(Status.READY, Status.ACTIVE)) {
this.serverSocket = this.serverSocketFactory.createServerSocket(
Expand All @@ -147,14 +149,16 @@ public void start() throws IOException {
if (this.socketConfig.getRcvBufSize() > 0) {
this.serverSocket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
}
if (this.socketConfig.getTcpKeepIdle() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
}
if (this.socketConfig.getTcpKeepInterval() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
}
if (this.socketConfig.getTcpKeepCount() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
if (ReflectionUtils.supportsKeepAliveOptions()) {
if (this.socketConfig.getTcpKeepIdle() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
}
if (this.socketConfig.getTcpKeepInterval() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
}
if (this.socketConfig.getTcpKeepCount() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
}
}
if (this.sslSetupHandler != null && this.serverSocket instanceof SSLServerSocket) {
final SSLServerSocket sslServerSocket = (SSLServerSocket) this.serverSocket;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.apache.hc.core5.http.io.HttpServerConnection;
import org.apache.hc.core5.http.io.SocketConfig;
import org.apache.hc.core5.io.Closer;
import org.apache.hc.core5.util.ReflectionUtils;

class RequestListener implements Runnable {

Expand Down Expand Up @@ -80,6 +81,7 @@ public RequestListener(
this.terminated = new AtomicBoolean();
}

@SuppressWarnings("Since15")
private HttpServerConnection createConnection(final Socket socket) throws IOException {
socket.setSoTimeout(this.socketConfig.getSoTimeout().toMillisecondsIntBound());
socket.setKeepAlive(this.socketConfig.isSoKeepAlive());
Expand All @@ -93,14 +95,16 @@ private HttpServerConnection createConnection(final Socket socket) throws IOExce
if (this.socketConfig.getSoLinger().toSeconds() >= 0) {
socket.setSoLinger(true, this.socketConfig.getSoLinger().toSecondsIntBound());
}
if (this.socketConfig.getTcpKeepIdle() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
}
if (this.socketConfig.getTcpKeepInterval() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
}
if (this.socketConfig.getTcpKeepCount() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
if (ReflectionUtils.supportsKeepAliveOptions()) {
if (this.socketConfig.getTcpKeepIdle() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
}
if (this.socketConfig.getTcpKeepInterval() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
}
if (this.socketConfig.getTcpKeepCount() > 0) {
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
}
}
if (!(socket instanceof SSLSocket) && sslSocketFactory != null) {
final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, null, -1, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.hc.core5.io.Closer;
import org.apache.hc.core5.net.NamedEndpoint;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.ReflectionUtils;
import org.apache.hc.core5.util.Timeout;

class SingleCoreIOReactor extends AbstractSingleCoreIOReactor implements ConnectionInitiator, IOWorkerStats {
Expand Down Expand Up @@ -278,6 +279,7 @@ public Future<IOSession> connect(
return sessionRequest;
}

@SuppressWarnings("Since15")
private void prepareSocket(final SocketChannel socketChannel) throws IOException {
if (this.reactorConfig.getSndBufSize() > 0) {
socketChannel.setOption(StandardSocketOptions.SO_SNDBUF, this.reactorConfig.getSndBufSize());
Expand All @@ -300,14 +302,16 @@ private void prepareSocket(final SocketChannel socketChannel) throws IOException
if (this.reactorConfig.getTrafficClass() > 0) {
socketChannel.setOption(StandardSocketOptions.IP_TOS, this.reactorConfig.getTrafficClass());
}
if (this.reactorConfig.getTcpKeepIdle() > 0) {
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, this.reactorConfig.getTcpKeepIdle());
}
if (this.reactorConfig.getTcpKeepInterval() > 0) {
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, this.reactorConfig.getTcpKeepInterval());
}
if (this.reactorConfig.getTcpKeepCount() > 0) {
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, this.reactorConfig.getTcpKeepCount());
if (ReflectionUtils.supportsKeepAliveOptions()) {
if (this.reactorConfig.getTcpKeepIdle() > 0) {
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, this.reactorConfig.getTcpKeepIdle());
}
if (this.reactorConfig.getTcpKeepInterval() > 0) {
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, this.reactorConfig.getTcpKeepInterval());
}
if (this.reactorConfig.getTcpKeepCount() > 0) {
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, this.reactorConfig.getTcpKeepCount());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@

package org.apache.hc.core5.util;

import java.lang.reflect.Method;

import jdk.net.ExtendedSocketOptions;
import jdk.net.Sockets;
import org.apache.hc.core5.annotation.Internal;

import java.lang.reflect.Method;
import java.net.Socket;
import java.util.Arrays;

@Internal
@SuppressWarnings("Since15")
public final class ReflectionUtils {
private static final boolean SUPPORTS_KEEPALIVE_OPTIONS = Sockets.supportedOptions(Socket.class)
.containsAll(Arrays.asList(ExtendedSocketOptions.TCP_KEEPIDLE, ExtendedSocketOptions.TCP_KEEPINTERVAL,
ExtendedSocketOptions.TCP_KEEPCOUNT));

public static void callSetter(final Object object, final String setterName, final Class<?> type, final Object value) {
try {
Expand Down Expand Up @@ -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.

return SUPPORTS_KEEPALIVE_OPTIONS;
}
}
Loading