public CircuitBreakerFactory(IMetricEvents metricEvents, IFailurePercentageCircuitBreakerConfig breakerConfig, IMjolnirLogFactory logFactory) { _metricEvents = metricEvents ?? throw new ArgumentNullException(nameof(metricEvents)); _breakerConfig = breakerConfig ?? throw new ArgumentNullException(nameof(breakerConfig)); _logFactory = logFactory ?? throw new ArgumentNullException(nameof(logFactory)); _log = logFactory.CreateLog <FailurePercentageCircuitBreaker>(); if (_log == null) { throw new InvalidOperationException($"{nameof(IMjolnirLogFactory)} implementation returned null from {nameof(IMjolnirLogFactory.CreateLog)} for type {typeof(CircuitBreakerFactory)}, please make sure the implementation returns a non-null log for all calls to {nameof(IMjolnirLogFactory.CreateLog)}"); } _timer = new GaugeTimer(state => { try { var keys = _circuitBreakers.Keys; foreach (var key in keys) { if (_circuitBreakers.TryGetValue(key, out Lazy <FailurePercentageCircuitBreaker> lazy) && lazy.IsValueCreated) { var breaker = lazy.Value; _metricEvents.BreakerGauge( breaker.Name, _breakerConfig.GetMinimumOperations(key), _breakerConfig.GetWindowMillis(key), _breakerConfig.GetThresholdPercentage(key), _breakerConfig.GetTrippedDurationMillis(key), _breakerConfig.GetForceTripped(key), _breakerConfig.GetForceFixed(key), breaker.IsTripped(), breaker.Metrics.SuccessCount, breaker.Metrics.FailureCount); } } } catch (Exception e) { _log.Error($"Error sending {nameof(IMetricEvents.BreakerGauge)} metric event", e); } }); }
private bool IsPastWaitDuration() { return(_clock.GetMillisecondTimestamp() > _lastTrippedTimestamp + _config.GetTrippedDurationMillis(_key)); }