Last active
August 1, 2023 09:07
-
-
Save hyrmn/a5227ed08923f3d14bab7736a9683c24 to your computer and use it in GitHub Desktop.
Polly.Contrib.WaitAndRetry examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
It definitely sounds like it should @HarisMQ. Good catch. The code above only deals with intermittent connectivity issues to upstream services and doesn't account for issues like DNS name resolution. You could add it or add a specific policy for things that may take longer to resolve and then use
Policy.Wrap
to combine them.It's also worth exploring the circuit-breaker pattern and the support for that in Polly as you might want to break on something like your internet connection being down so you can provide better messaging back to your users.