|
33 | 33 | import java.net.Socket;
|
34 | 34 | import java.net.SocketAddress;
|
35 | 35 | import java.nio.file.Path;
|
| 36 | +import java.util.Arrays; |
36 | 37 | import java.util.Collections;
|
37 | 38 | import java.util.List;
|
38 | 39 |
|
|
79 | 80 | public class DefaultHttpClientConnectionOperator implements HttpClientConnectionOperator {
|
80 | 81 |
|
81 | 82 | private static final Logger LOG = LoggerFactory.getLogger(DefaultHttpClientConnectionOperator.class);
|
| 83 | + @SuppressWarnings("Since15") |
| 84 | + private static final boolean SUPPORTS_KEEPALIVE_OPTIONS = Sockets.supportedOptions(Socket.class) |
| 85 | + .containsAll(Arrays.asList(ExtendedSocketOptions.TCP_KEEPIDLE, ExtendedSocketOptions.TCP_KEEPINTERVAL, |
| 86 | + ExtendedSocketOptions.TCP_KEEPCOUNT)); |
82 | 87 |
|
83 | 88 | static final DetachedSocketFactory PLAIN_SOCKET_FACTORY = socksProxy -> socksProxy == null ? new Socket() : new Socket(socksProxy);
|
84 | 89 |
|
@@ -319,14 +324,16 @@ private static void configureSocket(final Socket socket, final SocketConfig sock
|
319 | 324 | if (linger >= 0) {
|
320 | 325 | socket.setSoLinger(true, linger);
|
321 | 326 | }
|
322 |
| - if (socketConfig.getTcpKeepIdle() > 0) { |
323 |
| - Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPIDLE, socketConfig.getTcpKeepIdle()); |
324 |
| - } |
325 |
| - if (socketConfig.getTcpKeepInterval() > 0) { |
326 |
| - Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPINTERVAL, socketConfig.getTcpKeepInterval()); |
327 |
| - } |
328 |
| - if (socketConfig.getTcpKeepCount() > 0) { |
329 |
| - Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPCOUNT, socketConfig.getTcpKeepCount()); |
| 327 | + if (SUPPORTS_KEEPALIVE_OPTIONS) { |
| 328 | + if (socketConfig.getTcpKeepIdle() > 0) { |
| 329 | + Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPIDLE, socketConfig.getTcpKeepIdle()); |
| 330 | + } |
| 331 | + if (socketConfig.getTcpKeepInterval() > 0) { |
| 332 | + Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPINTERVAL, socketConfig.getTcpKeepInterval()); |
| 333 | + } |
| 334 | + if (socketConfig.getTcpKeepCount() > 0) { |
| 335 | + Sockets.setOption(socket, ExtendedSocketOptions.TCP_KEEPCOUNT, socketConfig.getTcpKeepCount()); |
| 336 | + } |
330 | 337 | }
|
331 | 338 | }
|
332 | 339 |
|
|
0 commit comments