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