private Policy <IRestResponse> CreateRetryWithTimeoutPolicy() { var retryWithTimeoutPolicy = Policy.HandleResult <IRestResponse>(r => StatusCodesWorthRetrying.Contains(r.StatusCode) || r.ErrorException != null) .WaitAndRetry(new[] { TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2), TimeSpan.FromSeconds(3) }, (exception, timeSpan, _attemptNo, context) => { _logger.Warning($"Previous attempt failed, trying again (attempt no #{_attemptNo} in {timeSpan}"); }); return(retryWithTimeoutPolicy); }
private Policy <IRestResponse> CreateCircuitBreakerPolicy() { Action <DelegateResult <IRestResponse>, TimeSpan> OnBreak = (_response, _timespan) => _logger.Warning($"Circuit breaker triggered because of too many failures. Will open after {_timespan}"); Action OnReset = () => _logger.Warning("Circuit is closed again."); Action OnHalfOpen = () => _logger.Warning("Circuit is now half open."); var circuitBreakerPolicy = Policy.HandleResult <IRestResponse>(r => StatusCodesWorthRetrying.Contains(r.StatusCode) || r.ErrorException != null) .CircuitBreaker( handledEventsAllowedBeforeBreaking: _resilienceSettings.BreakerAttemptThreshold, durationOfBreak: TimeSpan.FromSeconds(_resilienceSettings.CircuitBreakerTrippedTimeoutInSeconds), onBreak: OnBreak, onReset: OnReset, onHalfOpen: OnHalfOpen); return(circuitBreakerPolicy); }