Skip to content

Commit 49f1827

Browse files
committed
Discontinue use of SocketSupport to set extended socket options
The extended socket options related to TCP Keep-Alive were backported to Java 8 over five years ago. We can simply enable them directly without having to use reflection.
1 parent 968a56e commit 49f1827

File tree

7 files changed

+20
-218
lines changed

7 files changed

+20
-218
lines changed

httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpRequester.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import javax.net.ssl.SSLSocket;
4646
import javax.net.ssl.SSLSocketFactory;
4747

48+
import jdk.net.ExtendedSocketOptions;
49+
import jdk.net.Sockets;
4850
import org.apache.hc.core5.annotation.Internal;
4951
import org.apache.hc.core5.function.Callback;
5052
import org.apache.hc.core5.function.Resolver;
@@ -76,7 +78,6 @@
7678
import org.apache.hc.core5.io.CloseMode;
7779
import org.apache.hc.core5.io.Closer;
7880
import org.apache.hc.core5.io.ModalCloseable;
79-
import org.apache.hc.core5.io.SocketSupport;
8081
import org.apache.hc.core5.net.URIAuthority;
8182
import org.apache.hc.core5.pool.ConnPoolControl;
8283
import org.apache.hc.core5.pool.ManagedConnPool;
@@ -251,13 +252,13 @@ private HttpClientConnection createConnection(final Socket sock, final HttpHost
251252
sock.setSendBufferSize(socketConfig.getSndBufSize());
252253
}
253254
if (this.socketConfig.getTcpKeepIdle() > 0) {
254-
SocketSupport.setOption(sock, SocketSupport.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
255+
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
255256
}
256257
if (this.socketConfig.getTcpKeepInterval() > 0) {
257-
SocketSupport.setOption(sock, SocketSupport.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
258+
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
258259
}
259260
if (this.socketConfig.getTcpKeepCount() > 0) {
260-
SocketSupport.setOption(sock, SocketSupport.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
261+
Sockets.setOption(sock, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
261262
}
262263
final int linger = socketConfig.getSoLinger().toMillisecondsIntBound();
263264
if (linger >= 0) {

httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/HttpServer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import javax.net.ssl.SSLServerSocket;
4242
import javax.net.ssl.SSLServerSocketFactory;
4343

44+
import jdk.net.ExtendedSocketOptions;
45+
import jdk.net.Sockets;
4446
import org.apache.hc.core5.annotation.Internal;
4547
import org.apache.hc.core5.concurrent.DefaultThreadFactory;
4648
import org.apache.hc.core5.function.Callback;
@@ -56,7 +58,6 @@
5658
import org.apache.hc.core5.io.CloseMode;
5759
import org.apache.hc.core5.io.Closer;
5860
import org.apache.hc.core5.io.ModalCloseable;
59-
import org.apache.hc.core5.io.SocketSupport;
6061
import org.apache.hc.core5.util.Args;
6162
import org.apache.hc.core5.util.TimeValue;
6263
import org.apache.hc.core5.util.Timeout;
@@ -147,13 +148,13 @@ public void start() throws IOException {
147148
this.serverSocket.setReceiveBufferSize(this.socketConfig.getRcvBufSize());
148149
}
149150
if (this.socketConfig.getTcpKeepIdle() > 0) {
150-
SocketSupport.setOption(this.serverSocket, SocketSupport.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
151+
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
151152
}
152153
if (this.socketConfig.getTcpKeepInterval() > 0) {
153-
SocketSupport.setOption(this.serverSocket, SocketSupport.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
154+
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
154155
}
155156
if (this.socketConfig.getTcpKeepCount() > 0) {
156-
SocketSupport.setOption(this.serverSocket, SocketSupport.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
157+
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
157158
}
158159
if (this.sslSetupHandler != null && this.serverSocket instanceof SSLServerSocket) {
159160
final SSLServerSocket sslServerSocket = (SSLServerSocket) this.serverSocket;

httpcore5/src/main/java/org/apache/hc/core5/http/impl/bootstrap/RequestListener.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@
3838
import javax.net.ssl.SSLSocket;
3939
import javax.net.ssl.SSLSocketFactory;
4040

41+
import jdk.net.ExtendedSocketOptions;
42+
import jdk.net.Sockets;
4143
import org.apache.hc.core5.function.Callback;
4244
import org.apache.hc.core5.http.ExceptionListener;
4345
import org.apache.hc.core5.http.impl.io.HttpService;
4446
import org.apache.hc.core5.http.io.HttpConnectionFactory;
4547
import org.apache.hc.core5.http.io.HttpServerConnection;
4648
import org.apache.hc.core5.http.io.SocketConfig;
4749
import org.apache.hc.core5.io.Closer;
48-
import org.apache.hc.core5.io.SocketSupport;
4950

5051
class RequestListener implements Runnable {
5152

@@ -93,13 +94,13 @@ private HttpServerConnection createConnection(final Socket socket) throws IOExce
9394
socket.setSoLinger(true, this.socketConfig.getSoLinger().toSecondsIntBound());
9495
}
9596
if (this.socketConfig.getTcpKeepIdle() > 0) {
96-
SocketSupport.setOption(this.serverSocket, SocketSupport.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
97+
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPIDLE, this.socketConfig.getTcpKeepIdle());
9798
}
9899
if (this.socketConfig.getTcpKeepInterval() > 0) {
99-
SocketSupport.setOption(this.serverSocket, SocketSupport.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
100+
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPINTERVAL, this.socketConfig.getTcpKeepInterval());
100101
}
101102
if (this.socketConfig.getTcpKeepCount() > 0) {
102-
SocketSupport.setOption(this.serverSocket, SocketSupport.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
103+
Sockets.setOption(this.serverSocket, ExtendedSocketOptions.TCP_KEEPCOUNT, this.socketConfig.getTcpKeepCount());
103104
}
104105
if (!(socket instanceof SSLSocket) && sslSocketFactory != null) {
105106
final SSLSocket sslSocket = (SSLSocket) sslSocketFactory.createSocket(socket, null, -1, false);

httpcore5/src/main/java/org/apache/hc/core5/io/SocketSupport.java

Lines changed: 0 additions & 80 deletions
This file was deleted.

httpcore5/src/main/java/org/apache/hc/core5/reactor/SingleCoreIOReactor.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.net.ProtocolFamily;
3333
import java.net.Socket;
3434
import java.net.SocketAddress;
35-
import java.net.SocketOption;
3635
import java.net.StandardProtocolFamily;
3736
import java.net.StandardSocketOptions;
3837
import java.net.UnknownHostException;
@@ -48,12 +47,12 @@
4847
import java.util.concurrent.atomic.AtomicInteger;
4948
import java.util.concurrent.atomic.AtomicLong;
5049

50+
import jdk.net.ExtendedSocketOptions;
5151
import org.apache.hc.core5.concurrent.FutureCallback;
5252
import org.apache.hc.core5.function.Callback;
5353
import org.apache.hc.core5.function.Decorator;
5454
import org.apache.hc.core5.io.CloseMode;
5555
import org.apache.hc.core5.io.Closer;
56-
import org.apache.hc.core5.io.SocketSupport;
5756
import org.apache.hc.core5.net.NamedEndpoint;
5857
import org.apache.hc.core5.util.Args;
5958
import org.apache.hc.core5.util.Timeout;
@@ -302,28 +301,16 @@ private void prepareSocket(final SocketChannel socketChannel) throws IOException
302301
socketChannel.setOption(StandardSocketOptions.IP_TOS, this.reactorConfig.getTrafficClass());
303302
}
304303
if (this.reactorConfig.getTcpKeepIdle() > 0) {
305-
setExtendedSocketOption(socketChannel, SocketSupport.TCP_KEEPIDLE, this.reactorConfig.getTcpKeepIdle());
304+
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, this.reactorConfig.getTcpKeepIdle());
306305
}
307306
if (this.reactorConfig.getTcpKeepInterval() > 0) {
308-
setExtendedSocketOption(socketChannel, SocketSupport.TCP_KEEPINTERVAL, this.reactorConfig.getTcpKeepInterval());
307+
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, this.reactorConfig.getTcpKeepInterval());
309308
}
310309
if (this.reactorConfig.getTcpKeepCount() > 0) {
311-
setExtendedSocketOption(socketChannel, SocketSupport.TCP_KEEPCOUNT, this.reactorConfig.getTcpKeepCount());
310+
socketChannel.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, this.reactorConfig.getTcpKeepCount());
312311
}
313312
}
314313

315-
/**
316-
* @since 5.3
317-
*/
318-
<T> void setExtendedSocketOption(final SocketChannel socketChannel,
319-
final String optionName, final T value) throws IOException {
320-
final SocketOption<T> socketOption = SocketSupport.getExtendedSocketOptionOrNull(optionName);
321-
if (socketOption == null) {
322-
throw new UnsupportedOperationException(optionName + " is not supported in the current jdk");
323-
}
324-
socketChannel.setOption(socketOption, value);
325-
}
326-
327314
private void validateAddress(final SocketAddress address) throws UnknownHostException {
328315
if (address instanceof InetSocketAddress) {
329316
final InetSocketAddress endpoint = (InetSocketAddress) address;

httpcore5/src/test/java/org/apache/hc/core5/io/TestSocketSupport.java

Lines changed: 0 additions & 108 deletions
This file was deleted.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
<rxjava3.version>3.1.10</rxjava3.version>
8282
<testcontainers.version>1.21.3</testcontainers.version>
8383
<api.comparison.version>5.3</api.comparison.version>
84-
<hc.animal-sniffer.signature.ignores>javax.net.ssl.SSLEngine,javax.net.ssl.SSLParameters,java.nio.ByteBuffer,java.nio.CharBuffer</hc.animal-sniffer.signature.ignores>
84+
<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>
8585
</properties>
8686

8787
<dependencyManagement>

0 commit comments

Comments
 (0)