Polly.Contrib.WaitAndRetry examples
var transientErrors = new HashSet<WebExceptionStatus>(new[] | |
{ | |
WebExceptionStatus.ConnectFailure, | |
WebExceptionStatus.ConnectionClosed, | |
WebExceptionStatus.KeepAliveFailure, | |
WebExceptionStatus.Pending, | |
WebExceptionStatus.PipelineFailure, | |
WebExceptionStatus.ProtocolError, | |
WebExceptionStatus.ReceiveFailure, | |
WebExceptionStatus.RequestCanceled, | |
WebExceptionStatus.SecureChannelFailure, | |
WebExceptionStatus.SendFailure, | |
WebExceptionStatus.Timeout | |
}); | |
var retryTimes = new List<TimeSpan> | |
{ | |
TimeSpan.FromMilliseconds(0), | |
TimeSpan.FromMilliseconds(50), | |
TimeSpan.FromMilliseconds(100), | |
TimeSpan.FromMilliseconds(150) | |
}; | |
var retryPolicy = Policy | |
.Handle<WebException>(ex => transientErrors.Contains(ex.Status)) | |
.OrInner<WebException>(ex => transientErrors.Contains(ex.Status)) | |
.Or<TimeoutException>() | |
.OrInner<TimeoutException>() | |
.WaitAndRetry(retryTimes, | |
(exception, timeSpan, retryCount, context) => | |
{ | |
LogTo.Warning( | |
exception, | |
"WARNING: Unable to save to Raven, will retry after {RetryTimeSpan}, Retry attempt {RetryCount}", | |
timeSpan, | |
retryCount); | |
}); |
var constant = Backoff.ConstantBackoff(TimeSpan.FromMilliseconds(100), retryCount: 5, fastFirst: true); | |
var constantPolicy = Policy | |
.Handle<FooException>() | |
.WaitAndRetryAsync(constant); | |
var linear = Backoff.LinearBackoff(TimeSpan.FromMilliseconds(50), retryCount: 5, fastFirst: true); | |
var linearPolicy = Policy | |
.Handle<FooException>() | |
.WaitAndRetryAsync(linear); | |
var exponential = Backoff.ExponentialBackoff(TimeSpan.FromMilliseconds(20), retryCount: 5, fastFirst: true); | |
var exponentialPolicy = Policy | |
.Handle<FooException>() | |
.WaitAndRetryAsync(exponential); | |
var jittered = Backoff.DecorrelatedJitterBackoffV2( | |
medianFirstDelay: TimeSpan.FromMilliseconds(50) | |
retryCount: 5, | |
fastFirst: true); | |
var jitteredPolicy = Policy | |
.Handle<FooException>() | |
.WaitAndRetryAsync(jittered); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment