public Heartbeat(ClusterState clusterState, ClusterMembers clusterMembers, ClusterMessaging clusterMessaging, HeartbeatOptions options, ILoggerFactory loggerFactory)
        {
            _clusterState     = clusterState ?? throw new ArgumentNullException(nameof(clusterState));
            _clusterMembers   = clusterMembers ?? throw new ArgumentNullException(nameof(clusterMembers));
            _clusterMessaging = clusterMessaging ?? throw new ArgumentNullException(nameof(clusterMessaging));
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            _logger = loggerFactory?.CreateLogger <Heartbeat>() ?? throw new ArgumentNullException(nameof(loggerFactory));

            _period      = TimeSpan.FromMilliseconds(options.PeriodMilliseconds);
            _timeout     = TimeSpan.FromMilliseconds(options.TimeoutMilliseconds);
            _pingTimeout = options.PingTimeoutMilliseconds;

            // sanity checks
            if (_timeout <= _period)
            {
                var timeout = TimeSpan.FromMilliseconds(2 * _period.TotalMilliseconds);
                _logger.LogWarning("Heartbeat timeout {Timeout}ms is <= period {Period}ms, falling back to a {Value}ms timeout.",
                                   _timeout, _period.TotalMilliseconds, timeout);
                _timeout = timeout;
            }

            if (_pingTimeout >= _period.TotalMilliseconds)
            {
                var pingTimeout = (int)_period.TotalMilliseconds / 2;
                _logger.LogWarning("Ping timeout {Timeout}ms is >= period {Period}ms, falling back to a {Value}ms timeout.",
                                   _pingTimeout, _period.TotalMilliseconds, pingTimeout);
                _pingTimeout = pingTimeout;
            }
        }
Beispiel #2
0
        public Heartbeat(ClusterState clusterState, ClusterMessaging clusterMessaging, HeartbeatOptions options, TerminateConnections terminateConnections)
        {
            _clusterState         = clusterState ?? throw new ArgumentNullException(nameof(clusterState));
            _clusterMessaging     = clusterMessaging ?? throw new ArgumentNullException(nameof(clusterMessaging));
            _terminateConnections = terminateConnections;
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _logger  = clusterState.LoggerFactory.CreateLogger <Heartbeat>(); // FIXME with client ID?
            _period  = TimeSpan.FromMilliseconds(options.PeriodMilliseconds);
            _timeout = TimeSpan.FromMilliseconds(options.TimeoutMilliseconds);

            if (options.PeriodMilliseconds < 0)
            {
                _logger.LogInformation("Heartbeat is disabled (period < 0)");
                return;
            }

            // sanity checks
            if (_timeout <= _period)
            {
                var timeout = TimeSpan.FromMilliseconds(2 * _period.TotalMilliseconds);
                _logger.LogWarning("Heartbeat timeout {Timeout}ms is <= period {Period}ms, falling back to a {Value}ms timeout.",
                                   _timeout, _period.TotalMilliseconds, timeout);
                _timeout = timeout;
            }

            _logger.LogInformation("Heartbeat with {Period}ms period and {Timeout}ms timeout",
                                   _period.ToString("hh\\:mm\\:ss", CultureInfo.InvariantCulture),
                                   _timeout.ToString("hh\\:mm\\:ss", CultureInfo.InvariantCulture));

            HConsole.Configure(x => x.Configure <Heartbeat>().SetPrefix("HEARTBEAT")); // FIXME with client ID?

            _cancel       = new CancellationTokenSource();
            _heartbeating = BeatAsync(_cancel.Token);
            _active       = 1;
        }
        public Heartbeat(ClusterState clusterState, ClusterMembers clusterMembers, ClusterMessaging clusterMessaging, HeartbeatOptions options, ILoggerFactory loggerFactory)
        {
            _clusterState     = clusterState ?? throw new ArgumentNullException(nameof(clusterState));
            _clusterMembers   = clusterMembers ?? throw new ArgumentNullException(nameof(clusterMembers));
            _clusterMessaging = clusterMessaging ?? throw new ArgumentNullException(nameof(clusterMessaging));
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            _logger = loggerFactory?.CreateLogger <Heartbeat>() ?? throw new ArgumentNullException(nameof(loggerFactory));

            _period  = TimeSpan.FromMilliseconds(options.PeriodMilliseconds);
            _timeout = TimeSpan.FromMilliseconds(options.TimeoutMilliseconds);

            // sanity checks
            if (_timeout <= _period)
            {
                var timeout = TimeSpan.FromMilliseconds(2 * _period.TotalMilliseconds);
                _logger.LogWarning("Heartbeat timeout {Timeout}ms is <= period {Period}ms, falling back to a {Value}ms timeout.",
                                   _timeout, _period.TotalMilliseconds, timeout);
                _timeout = timeout;
            }

            _cancellation = new CancellationTokenSource();
            _heartbeating ??= LoopAsync(_cancellation.Token);
        }