public RaftOptions() { Election = new Range() { FromMilliseconds = 150, ToMilliseconds = 300 }; Hartbeat = new Range() { FromMilliseconds = 1000, ToMilliseconds = 1500, }; FailureTolerance = 1; Ip4Only = true; AppendPort = 12346; VotePort = 12348; _ipAddress = new Lazy<IPAddress>(() => { var ip4Only = Ip4Only; var availableAddresses = Dns.GetHostAddresses(Dns.GetHostName()); return availableAddresses .FirstOrDefault( ip => ip.AddressFamily == (ip4Only ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6)); }); }
IDisposable Subscribe(IObserver<ServerStateType> observer, Range range) { _serverStateType = observer; _hartbeatTimeoutDispose?.Dispose(); var timeout = _random.Next(range.FromMilliseconds, range.ToMilliseconds); _hartbeatTimeout = Observable.Timer(TimeSpan.FromMilliseconds(timeout)); _hartbeatTimeoutDispose = _hartbeatTimeout.Subscribe((p) => { if (isStopped) return; if (_options.UseLogging) _logger.LogInformation($"Hartbeat Timeout {timeout}"); _serverStateType.OnNext(ServerStateType.Candidate); Subscribe(_serverStateType, range); }); return this; }