record RemoteClusterClientBootstrapOptions()

in x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/core/security/transport/netty4/SecurityNetty4Transport.java [365:519]


    record RemoteClusterClientBootstrapOptions(
        Boolean tcpNoDelay,
        Boolean tcpKeepAlive,
        Integer tcpKeepIdle,
        Integer tcpKeepInterval,
        Integer tcpKeepCount,
        ByteSizeValue tcpSendBufferSize,
        ByteSizeValue tcpReceiveBufferSize,
        Boolean tcpReuseAddress
    ) {

        boolean isEmpty() {
            return tcpNoDelay == null
                && tcpKeepAlive == null
                && tcpKeepIdle == null
                && tcpKeepInterval == null
                && tcpKeepCount == null
                && tcpSendBufferSize == null
                && tcpReceiveBufferSize == null
                && tcpReuseAddress == null;
        }

        void configure(Bootstrap bootstrap) {
            if (tcpNoDelay != null) {
                bootstrap.option(ChannelOption.TCP_NODELAY, tcpNoDelay);
            }

            if (tcpKeepAlive != null) {
                bootstrap.option(ChannelOption.SO_KEEPALIVE, tcpKeepAlive);
                if (tcpKeepAlive) {
                    // Note that Netty logs a warning if it can't set the option
                    if (tcpKeepIdle != null) {
                        if (tcpKeepIdle >= 0) {
                            bootstrap.option(OPTION_TCP_KEEP_IDLE, tcpKeepIdle);
                        } else {
                            bootstrap.option(OPTION_TCP_KEEP_IDLE, null);
                        }
                    }
                    if (tcpKeepInterval != null) {
                        if (tcpKeepInterval >= 0) {
                            bootstrap.option(OPTION_TCP_KEEP_INTERVAL, tcpKeepInterval);
                        } else {
                            bootstrap.option(OPTION_TCP_KEEP_INTERVAL, null);
                        }
                    }
                    if (tcpKeepCount != null) {
                        if (tcpKeepCount >= 0) {
                            bootstrap.option(OPTION_TCP_KEEP_COUNT, tcpKeepCount);
                        } else {
                            bootstrap.option(OPTION_TCP_KEEP_COUNT, null);
                        }
                    }
                } else {
                    bootstrap.option(OPTION_TCP_KEEP_IDLE, null);
                    bootstrap.option(OPTION_TCP_KEEP_INTERVAL, null);
                    bootstrap.option(OPTION_TCP_KEEP_COUNT, null);
                }
            }

            if (tcpSendBufferSize != null) {
                if (tcpSendBufferSize.getBytes() > 0) {
                    bootstrap.option(ChannelOption.SO_SNDBUF, Math.toIntExact(tcpSendBufferSize.getBytes()));
                } else {
                    bootstrap.option(ChannelOption.SO_SNDBUF, null);
                }
            }

            if (tcpReceiveBufferSize != null) {
                if (tcpReceiveBufferSize.getBytes() > 0) {
                    bootstrap.option(ChannelOption.SO_RCVBUF, Math.toIntExact(tcpReceiveBufferSize.getBytes()));
                } else {
                    bootstrap.option(ChannelOption.SO_RCVBUF, null);
                }
            }

            if (tcpReuseAddress != null) {
                bootstrap.option(ChannelOption.SO_REUSEADDR, tcpReuseAddress);
            }
        }

        static RemoteClusterClientBootstrapOptions fromSettings(Settings settings) {
            Boolean tcpNoDelay = RemoteClusterPortSettings.TCP_NO_DELAY.get(settings);
            if (tcpNoDelay == TransportSettings.TCP_NO_DELAY.get(settings)) {
                tcpNoDelay = null;
            }

            // It is possible that both default and _remote_cluster enable keepAlive but have different
            // values for either keepIdle, keepInterval or keepCount. In this case, we need have a
            // non-null value for keepAlive even it is the same between default and _remote_cluster.
            Boolean tcpKeepAlive = RemoteClusterPortSettings.TCP_KEEP_ALIVE.get(settings);
            Integer tcpKeepIdle = RemoteClusterPortSettings.TCP_KEEP_IDLE.get(settings);
            Integer tcpKeepInterval = RemoteClusterPortSettings.TCP_KEEP_INTERVAL.get(settings);
            Integer tcpKeepCount = RemoteClusterPortSettings.TCP_KEEP_COUNT.get(settings);
            final Boolean defaultTcpKeepAlive = TransportSettings.TCP_KEEP_ALIVE.get(settings);

            if (tcpKeepAlive) {
                if (defaultTcpKeepAlive) {
                    // Both profiles have keepAlive enabled, we need to check whether any keepIdle, keepInterval, keepCount is different
                    if (tcpKeepIdle.equals(TransportSettings.TCP_KEEP_IDLE.get(settings))) {
                        tcpKeepIdle = null;
                    }
                    if (tcpKeepInterval.equals(TransportSettings.TCP_KEEP_INTERVAL.get(settings))) {
                        tcpKeepInterval = null;
                    }
                    if (tcpKeepCount.equals(TransportSettings.TCP_KEEP_COUNT.get(settings))) {
                        tcpKeepCount = null;
                    }
                    if (tcpKeepIdle == null && tcpKeepInterval == null && tcpKeepCount == null) {
                        // If keepIdle, keepInterval, keepCount are all identical, keepAlive can be null as well.
                        // That is no need to update anything keepXxx related
                        tcpKeepAlive = null;
                    }
                }
            } else {
                if (false == defaultTcpKeepAlive) {
                    tcpKeepAlive = null;
                }
                // _remote_cluster has keepAlive disabled, all other keepXxx has no reason to exist
                tcpKeepIdle = null;
                tcpKeepInterval = null;
                tcpKeepCount = null;
            }

            assert (tcpKeepAlive == null && tcpKeepIdle == null && tcpKeepInterval == null && tcpKeepCount == null)
                || (tcpKeepAlive == false && tcpKeepIdle == null && tcpKeepInterval == null && tcpKeepCount == null)
                || (tcpKeepAlive && (tcpKeepIdle != null || tcpKeepInterval != null || tcpKeepCount != null))
                : "keepAlive == true must be accompanied with either keepIdle, keepInterval or keepCount change";

            ByteSizeValue tcpSendBufferSize = RemoteClusterPortSettings.TCP_SEND_BUFFER_SIZE.get(settings);
            if (tcpSendBufferSize.equals(TransportSettings.TCP_SEND_BUFFER_SIZE.get(settings))) {
                tcpSendBufferSize = null;
            }

            ByteSizeValue tcpReceiveBufferSize = RemoteClusterPortSettings.TCP_RECEIVE_BUFFER_SIZE.get(settings);
            if (tcpReceiveBufferSize.equals(TransportSettings.TCP_RECEIVE_BUFFER_SIZE.get(settings))) {
                tcpReceiveBufferSize = null;
            }

            Boolean tcpReuseAddress = RemoteClusterPortSettings.TCP_REUSE_ADDRESS.get(settings);
            if (tcpReuseAddress == TransportSettings.TCP_REUSE_ADDRESS.get(settings)) {
                tcpReuseAddress = null;
            }

            return new RemoteClusterClientBootstrapOptions(
                tcpNoDelay,
                tcpKeepAlive,
                tcpKeepIdle,
                tcpKeepInterval,
                tcpKeepCount,
                tcpSendBufferSize,
                tcpReceiveBufferSize,
                tcpReuseAddress
            );
        }
    }