|
13 | 13 | import java.net.InetAddress; |
14 | 14 | import java.net.InetSocketAddress; |
15 | 15 | import java.net.Socket; |
| 16 | +import java.net.SocketException; |
16 | 17 | import java.net.UnknownHostException; |
| 18 | +import java.util.Optional; |
| 19 | +import java.util.concurrent.atomic.AtomicBoolean; |
17 | 20 |
|
18 | 21 | import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.apache.logging.log4j.LogManager; |
| 23 | +import org.apache.logging.log4j.Logger; |
19 | 24 |
|
20 | 25 | import com.mirth.connect.util.TcpUtil; |
21 | 26 |
|
| 27 | +import jdk.net.ExtendedSocketOptions; |
| 28 | + |
22 | 29 | public class SocketUtil { |
23 | 30 |
|
| 31 | + private static final Logger logger = LogManager.getLogger(SocketUtil.class); |
| 32 | + |
| 33 | + static final String KEEPALIVE_IDLE_PROPERTY = "oie.tcp.keepalive.idle"; |
| 34 | + static final int DEFAULT_KEEPALIVE_IDLE = 120; |
| 35 | + private static final Optional<Integer> KEEPALIVE_IDLE = initKeepAliveIdle(); |
| 36 | + private static final AtomicBoolean keepAliveIdleFailed = new AtomicBoolean(); |
| 37 | + |
24 | 38 | public static Socket createSocket(TcpConfiguration configuration) throws UnknownHostException, IOException { |
25 | 39 | return createSocket(configuration, null); |
26 | 40 | } |
@@ -67,6 +81,72 @@ public static void connectSocket(Socket socket, String host, int port, int timeo |
67 | 81 | socket.connect(new InetSocketAddress(InetAddress.getByName(TcpUtil.getFixedHost(host)), port), timeout); |
68 | 82 | } |
69 | 83 |
|
| 84 | + /** |
| 85 | + * Loads this class, which decides and logs the keepalive idle time. Called when the TCP plugin |
| 86 | + * starts so the decision, and any error in the oie.tcp.keepalive.idle system property, is |
| 87 | + * logged at server startup instead of at the first TCP connection. |
| 88 | + */ |
| 89 | + public static void init() {} |
| 90 | + |
| 91 | + /** |
| 92 | + * Enables or disables TCP keepalive on a socket. When enabling, also lowers the idle time before |
| 93 | + * the first keepalive probe to the oie.tcp.keepalive.idle system property (in seconds) if the |
| 94 | + * OS default was higher when this class was loaded and the socket supports it. A value of 0 or |
| 95 | + * less leaves the OS default. |
| 96 | + */ |
| 97 | + public static void setKeepAlive(Socket socket, boolean keepAlive) throws SocketException { |
| 98 | + socket.setKeepAlive(keepAlive); |
| 99 | + |
| 100 | + if (keepAlive && KEEPALIVE_IDLE.isPresent() && socket.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { |
| 101 | + Integer idle = KEEPALIVE_IDLE.get(); |
| 102 | + try { |
| 103 | + socket.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, idle); |
| 104 | + } catch (IOException | IllegalArgumentException e) { |
| 105 | + if (keepAliveIdleFailed.compareAndSet(false, true)) { |
| 106 | + logger.error("Failed to set TCP_KEEPIDLE to {} seconds; using the OS default. Further failures will be logged at DEBUG.", idle, e); |
| 107 | + } else { |
| 108 | + logger.debug("Failed to set TCP_KEEPIDLE to {} seconds.", idle, e); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static Optional<Integer> initKeepAliveIdle() { |
| 115 | + int idle = DEFAULT_KEEPALIVE_IDLE; |
| 116 | + String value = System.getProperty(KEEPALIVE_IDLE_PROPERTY); |
| 117 | + if (value != null) { |
| 118 | + try { |
| 119 | + idle = Integer.parseInt(value.trim()); |
| 120 | + } catch (NumberFormatException e) { |
| 121 | + logger.error("Invalid value '{}' for {}; using {} seconds.", value, KEEPALIVE_IDLE_PROPERTY, DEFAULT_KEEPALIVE_IDLE); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (idle <= 0) { |
| 126 | + logger.info("TCP connectors use the OS keepalive idle time; {} is {}.", KEEPALIVE_IDLE_PROPERTY, idle); |
| 127 | + return Optional.empty(); |
| 128 | + } |
| 129 | + |
| 130 | + try (Socket socket = new Socket()) { |
| 131 | + if (!socket.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) { |
| 132 | + logger.info("TCP connectors use the OS keepalive idle time; TCP_KEEPIDLE is not supported."); |
| 133 | + return Optional.empty(); |
| 134 | + } |
| 135 | + |
| 136 | + int osIdle = socket.getOption(ExtendedSocketOptions.TCP_KEEPIDLE); |
| 137 | + if (osIdle <= idle) { |
| 138 | + logger.info("TCP connectors use the OS keepalive idle time; its current value of {} seconds is not higher than {} ({} seconds). Later changes to the OS value still apply, but {} is not compared again until the server restarts.", osIdle, KEEPALIVE_IDLE_PROPERTY, idle, KEEPALIVE_IDLE_PROPERTY); |
| 139 | + return Optional.empty(); |
| 140 | + } |
| 141 | + |
| 142 | + logger.info("TCP connectors use a keepalive idle time of {} seconds instead of the current OS value of {} seconds. Later changes to the OS value are ignored until the server restarts.", idle, osIdle); |
| 143 | + } catch (IOException e) { |
| 144 | + logger.info("TCP connectors use a keepalive idle time of {} seconds; the current OS value could not be read. Later changes to the OS value are ignored until the server restarts.", idle); |
| 145 | + } |
| 146 | + |
| 147 | + return Optional.of(idle); |
| 148 | + } |
| 149 | + |
70 | 150 | public static void closeSocket(Socket socket) throws IOException { |
71 | 151 | if (socket != null) { |
72 | 152 | /* |
|
0 commit comments