|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.events.zeromq; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.logging.Level; |
| 22 | +import java.util.logging.Logger; |
| 23 | +import org.zeromq.ZMQ; |
| 24 | + |
| 25 | +class ZmqUtils { |
| 26 | + |
| 27 | + private static final Logger LOG = Logger.getLogger(ZmqUtils.class.getName()); |
| 28 | + |
| 29 | + // Minimum heartbeat interval: 1 second |
| 30 | + private static final long MIN_HEARTBEAT_MS = 1_000L; |
| 31 | + // Maximum heartbeat interval: ~24 days (to prevent overflow when multiplied by 6) |
| 32 | + private static final long MAX_HEARTBEAT_MS = Integer.MAX_VALUE / 6; |
| 33 | + |
| 34 | + private ZmqUtils() {} |
| 35 | + |
| 36 | + /** |
| 37 | + * Configures ZeroMQ heartbeat settings on a socket to prevent stale connections. |
| 38 | + * |
| 39 | + * <p>The heartbeat interval is clamped between 1 second and ~24 days to prevent integer overflow |
| 40 | + * and ensure reasonable values. If the provided duration is outside this range, it will be |
| 41 | + * adjusted and a warning will be logged. |
| 42 | + * |
| 43 | + * @param socket The ZMQ socket to configure |
| 44 | + * @param heartbeatPeriod The heartbeat interval duration |
| 45 | + * @param socketType The socket type name for logging (e.g., "SUB", "PUB", "XPUB", "XSUB") |
| 46 | + */ |
| 47 | + static void configureHeartbeat(ZMQ.Socket socket, Duration heartbeatPeriod, String socketType) { |
| 48 | + if (heartbeatPeriod != null && !heartbeatPeriod.isZero() && !heartbeatPeriod.isNegative()) { |
| 49 | + long heartbeatMs = heartbeatPeriod.toMillis(); |
| 50 | + long clampedHeartbeatMs = clampHeartbeatInterval(heartbeatMs, socketType); |
| 51 | + |
| 52 | + // Safe to cast to int now |
| 53 | + int heartbeatIvl = (int) clampedHeartbeatMs; |
| 54 | + int heartbeatTimeout = heartbeatIvl * 3; |
| 55 | + int heartbeatTtl = heartbeatIvl * 6; |
| 56 | + |
| 57 | + socket.setHeartbeatIvl(heartbeatIvl); |
| 58 | + socket.setHeartbeatTimeout(heartbeatTimeout); |
| 59 | + socket.setHeartbeatTtl(heartbeatTtl); |
| 60 | + |
| 61 | + LOG.info( |
| 62 | + String.format( |
| 63 | + "ZMQ %s socket heartbeat configured: interval=%ds, timeout=%ds, ttl=%ds", |
| 64 | + socketType, heartbeatIvl / 1000, heartbeatTimeout / 1000, heartbeatTtl / 1000)); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Clamps the heartbeat interval to safe bounds and logs warnings if adjustments are made. |
| 70 | + * |
| 71 | + * @param heartbeatMs The heartbeat interval in milliseconds |
| 72 | + * @param socketType The socket type for logging |
| 73 | + * @return The clamped heartbeat interval |
| 74 | + */ |
| 75 | + private static long clampHeartbeatInterval(long heartbeatMs, String socketType) { |
| 76 | + if (heartbeatMs < MIN_HEARTBEAT_MS) { |
| 77 | + logHeartbeatClampWarning(socketType, heartbeatMs, MIN_HEARTBEAT_MS, "below minimum"); |
| 78 | + return MIN_HEARTBEAT_MS; |
| 79 | + } |
| 80 | + if (heartbeatMs > MAX_HEARTBEAT_MS) { |
| 81 | + logHeartbeatClampWarning(socketType, heartbeatMs, MAX_HEARTBEAT_MS, "exceeds maximum"); |
| 82 | + return MAX_HEARTBEAT_MS; |
| 83 | + } |
| 84 | + return heartbeatMs; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Logs a warning when the heartbeat interval is clamped. |
| 89 | + * |
| 90 | + * @param socketType The socket type |
| 91 | + * @param originalMs The original interval value in milliseconds |
| 92 | + * @param clampedMs The clamped interval value in milliseconds |
| 93 | + * @param reason The reason for clamping |
| 94 | + */ |
| 95 | + private static void logHeartbeatClampWarning( |
| 96 | + String socketType, long originalMs, long clampedMs, String reason) { |
| 97 | + LOG.log( |
| 98 | + Level.WARNING, |
| 99 | + String.format( |
| 100 | + "ZMQ %s socket heartbeat interval %ds is %s, clamping to %ds", |
| 101 | + socketType, originalMs / 1000, reason, clampedMs / 1000)); |
| 102 | + } |
| 103 | +} |
0 commit comments