Exemplo n.º 1
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and the current sleep duration.
        /// On each retry, the duration to wait is calculated by calling <paramref name="sleepDurationProvider"/> with
        /// the current retry attempt allowing an exponentially increasing wait time (exponential backoff).
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="sleepDurationProvider">The function that provides the duration to wait for for a particular retry attempt.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than zero.</exception>
        /// <exception cref="System.ArgumentNullException">
        /// timeSpanProvider
        /// or
        /// onRetry
        /// </exception>
        public static Policy 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 zero.");
            }
            if (sleepDurationProvider == null)
            {
                throw new ArgumentNullException("sleepDurationProvider");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            var sleepDurations = Enumerable.Range(1, retryCount)
                                 .Select(sleepDurationProvider);

            return(new Policy(
                       action => RetryPolicy.Implementation(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithSleep(sleepDurations, onRetry)
                           ),
                       policyBuilder.ExceptionPredicates
                       ));
        }
Exemplo n.º 2
0
        /// <summary> Builds <see cref="ActionPolicy"/> that will keep retrying forever </summary>
        /// <param name="syntax">The syntax to extend.</param>
        /// <param name="onRetry">The action to perform when the exception could be retried.</param>
        /// <returns> reusable instance of policy</returns>
        public static ActionPolicy RetryForever(this Syntax <ExceptionHandler> syntax, Action <Exception> onRetry)
        {
            Enforce.Arguments(() => syntax, () => onRetry);

            var state = new RetryState(onRetry);

            return(new ActionPolicy(action => RetryPolicy.Implementation(action, syntax.Target, () => state)));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Builds <see cref="ActionPolicy"/> that will retry exception handling
        /// for a couple of times before giving up.
        /// </summary>
        /// <param name="syntax">The syntax.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <returns>reusable instance of policy</returns>
        public static ActionPolicy Retry(this Syntax <ExceptionHandler> syntax, int retryCount)
        {
            Enforce.Argument(() => syntax);

            Func <IRetryState> state = () => new RetryStateWithCount(retryCount, DoNothing2);

            return(new ActionPolicy(action => RetryPolicy.Implementation(action, syntax.Target, state)));
        }
Exemplo n.º 4
0
        /// <summary> <para>Builds the policy that will keep retrying as long as
        /// the exception could be handled by the <paramref name="syntax"/> being
        /// built and <paramref name="sleepDurations"/> is providing the sleep intervals.
        /// </para>
        /// <para>See <see cref="Range"/> for methods to create long intervals on-the-fly</para>
        /// </summary>
        /// <param name="syntax">The syntax.</param>
        /// <param name="sleepDurations">The sleep durations.</param>
        /// <returns>new policy instance</returns>
        public static ActionPolicy WaitAndRetry(this Syntax <ExceptionHandler> syntax, IEnumerable <TimeSpan> sleepDurations)
        {
            Enforce.Arguments(() => syntax, () => sleepDurations);

            Func <IRetryState> state = () => new RetryStateWithSleep(sleepDurations, DoNothing2);

            return(new ActionPolicy(action => RetryPolicy.Implementation(action, syntax.Target, state)));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Builds <see cref="ActionPolicy"/> that will retry exception handling
        /// for a couple of times before giving up.
        /// </summary>
        /// <param name="syntax">The syntax.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to perform on retry (i.e.: write to log).
        /// First parameter is the exception and second one is its number in sequence. </param>
        /// <returns>reusable policy instance </returns>
        public static ActionPolicy Retry(this Syntax <ExceptionHandler> syntax, int retryCount, Action <Exception, int> onRetry)
        {
            Enforce.Arguments(() => syntax, () => onRetry);
            Enforce.Argument(() => retryCount, Is.GreaterThan(0));

            Func <IRetryState> state = () => new RetryStateWithCount(retryCount, onRetry);

            return(new ActionPolicy(action => RetryPolicy.Implementation(action, syntax.Target, state)));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and
        /// execution context.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static ContextualPolicy RetryForever(this PolicyBuilder policyBuilder, Action <Exception, Context> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new ContextualPolicy((action, context) => RetryPolicy.Implementation(
                                            action,
                                            policyBuilder.ExceptionPredicates,
                                            () => new RetryPolicyState(onRetry, context)
                                            )));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry indefinitely
        /// calling <paramref name="onRetry"/> on each retry with the raised exception.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static Policy RetryForever(this PolicyBuilder policyBuilder, Action <Exception> onRetry)
        {
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new Policy(
                       action => RetryPolicy.Implementation(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyState(onRetry)
                           ),
                       policyBuilder.ExceptionPredicates
                       ));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will retry <paramref name="retryCount"/> times
        /// calling <paramref name="onRetry"/> on each retry with the raised exception and retry count.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="retryCount">The retry count.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentOutOfRangeException">retryCount;Value must be greater than zero.</exception>
        /// <exception cref="System.ArgumentNullException">onRetry</exception>
        public static Policy Retry(this PolicyBuilder policyBuilder, int retryCount, Action <Exception, int> onRetry)
        {
            if (retryCount <= 0)
            {
                throw new ArgumentOutOfRangeException("retryCount", "Value must be greater than zero.");
            }
            if (onRetry == null)
            {
                throw new ArgumentNullException("onRetry");
            }

            return(new Policy(
                       action => RetryPolicy.Implementation(
                           action,
                           policyBuilder.ExceptionPredicates,
                           () => new RetryPolicyStateWithCount(retryCount, onRetry))));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Builds a <see cref="Policy"/> that will wait and retry as many times as there are provided <paramref name="sleepDurations"/>
        /// calling <paramref name="onRetry"/> on each retry with the raised exception, current sleep duration and
        /// execution context.
        /// On each retry, the duration to wait is the current <paramref name="sleepDurations"/> item.
        /// </summary>
        /// <param name="policyBuilder">The policy builder.</param>
        /// <param name="sleepDurations">The sleep durations to wait for on each retry.</param>
        /// <param name="onRetry">The action to call on each retry.</param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// sleepDurations
        /// or
        /// onRetry
        /// </exception>
        public static ContextualPolicy 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 ContextualPolicy((action, context) => RetryPolicy.Implementation(
                                            action,
                                            policyBuilder.ExceptionPredicates,
                                            () => new RetryPolicyStateWithSleep(sleepDurations, onRetry, context)
                                            )));
        }