RetrySyntax
Fluent API for defining a Retry Policy.
using Polly.Retry;
using Polly.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Polly
{
public static class RetrySyntax
{
public static RetryPolicy Retry(this PolicyBuilder policyBuilder)
{
return policyBuilder.Retry(1);
}
public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount)
{
Action<Exception, int> onRetry = delegate {
};
return policyBuilder.Retry(retryCount, onRetry);
}
public static RetryPolicy Retry(this PolicyBuilder policyBuilder, Action<Exception, int> onRetry)
{
return policyBuilder.Retry(1, onRetry);
}
public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int> onRetry)
{
if (retryCount < 0)
throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithCount<EmptyStruct>(retryCount, delegate(DelegateResult<EmptyStruct> outcome, int i, Context ctx) {
onRetry(outcome.Exception, i);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy Retry(this PolicyBuilder policyBuilder, Action<Exception, int, Context> onRetry)
{
return policyBuilder.Retry(1, onRetry);
}
public static RetryPolicy Retry(this PolicyBuilder policyBuilder, int retryCount, Action<Exception, int, Context> onRetry)
{
if (retryCount < 0)
throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithCount<EmptyStruct>(retryCount, delegate(DelegateResult<EmptyStruct> outcome, int i, Context ctx) {
onRetry(outcome.Exception, i, ctx);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder)
{
Action<Exception> onRetry = delegate {
};
return policyBuilder.RetryForever(onRetry);
}
public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action<Exception> onRetry)
{
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyState<EmptyStruct>(delegate(DelegateResult<EmptyStruct> outcome, Context ctx) {
onRetry(outcome.Exception);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy RetryForever(this PolicyBuilder policyBuilder, Action<Exception, Context> onRetry)
{
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyState<EmptyStruct>(delegate(DelegateResult<EmptyStruct> outcome, Context ctx) {
onRetry(outcome.Exception, ctx);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider)
{
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetry(retryCount, sleepDurationProvider, onRetry);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry)
{
if (retryCount < 0)
throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
IEnumerable<TimeSpan> sleepDurations = Enumerable.Range(1, retryCount).Select(sleepDurationProvider);
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleep<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, Context ctx) {
onRetry(outcome.Exception, timespan);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry)
{
if (retryCount < 0)
throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
IEnumerable<TimeSpan> sleepDurations = Enumerable.Range(1, retryCount).Select(sleepDurationProvider);
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleep<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, Context ctx) {
onRetry(outcome.Exception, timespan, ctx);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, int retryCount, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, int, Context> onRetry)
{
if (retryCount < 0)
throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than or equal to zero.");
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
IEnumerable<TimeSpan> sleepDurations = Enumerable.Range(1, retryCount).Select(sleepDurationProvider);
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleep<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, int i, Context ctx) {
onRetry(outcome.Exception, timespan, i, ctx);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations)
{
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetry(sleepDurations, onRetry);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan> onRetry)
{
if (sleepDurations == null)
throw new ArgumentNullException("sleepDurations");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleep<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, Context ctx) {
onRetry(outcome.Exception, timespan);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan, Context> onRetry)
{
if (sleepDurations == null)
throw new ArgumentNullException("sleepDurations");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleep<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, Context ctx) {
onRetry(outcome.Exception, timespan, ctx);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetry(this PolicyBuilder policyBuilder, IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan, int, Context> onRetry)
{
if (sleepDurations == null)
throw new ArgumentNullException("sleepDurations");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleep<EmptyStruct>(sleepDurations, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, int i, Context ctx) {
onRetry(outcome.Exception, timespan, i, ctx);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider)
{
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
Action<Exception, TimeSpan> onRetry = delegate {
};
return policyBuilder.WaitAndRetryForever(sleepDurationProvider, onRetry);
}
public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan> onRetry)
{
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleepDurationProvider<EmptyStruct>(sleepDurationProvider, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, Context ctx) {
onRetry(outcome.Exception, timespan);
}, context));
}, policyBuilder.ExceptionPredicates);
}
public static RetryPolicy WaitAndRetryForever(this PolicyBuilder policyBuilder, Func<int, TimeSpan> sleepDurationProvider, Action<Exception, TimeSpan, Context> onRetry)
{
if (sleepDurationProvider == null)
throw new ArgumentNullException("sleepDurationProvider");
if (onRetry == null)
throw new ArgumentNullException("onRetry");
return new RetryPolicy(delegate(Action<CancellationToken> action, Context context, CancellationToken cancellationToken) {
RetryEngine.Implementation(delegate(CancellationToken ct) {
action(ct);
return EmptyStruct.Instance;
}, cancellationToken, policyBuilder.ExceptionPredicates, PredicateHelper<EmptyStruct>.EmptyResultPredicates, () => new RetryPolicyStateWithSleepDurationProvider<EmptyStruct>(sleepDurationProvider, delegate(DelegateResult<EmptyStruct> outcome, TimeSpan timespan, Context ctx) {
onRetry(outcome.Exception, timespan, ctx);
}, context));
}, policyBuilder.ExceptionPredicates);
}
}
}