Esempio n. 1
0
        /// <summary>
        /// Modifies the attempts enumerable using the specified <see cref="IRetryStrategy"/>.
        /// </summary>
        public static IEnumerable <Lazy <T> > UsingStrategy <T>(this IEnumerable <Lazy <T> > lazyAttempts,
                                                                IRetryStrategy strategy, CancellationToken cancellationToken) where T : Attempt
        {
            if (strategy == null)
            {
                throw new ArgumentNullException(nameof(strategy));
            }

            if (strategy.AttemptLimit > 0)
            {
                lazyAttempts = lazyAttempts.Take(strategy.AttemptLimit.Value);
            }

            if (strategy.MaxDuration > TimeSpan.Zero)
            {
                lazyAttempts = lazyAttempts.TakeForDuration(strategy.MaxDuration.Value, cancellationToken);
            }

            if (strategy.FailureDelay > TimeSpan.Zero || strategy.FailureDelayAdjustment != null)
            {
                lazyAttempts = lazyAttempts.DelayWhereFailed(strategy.FailureDelay, strategy.FailureDelayAdjustment,
                                                             cancellationToken);
            }

            if (strategy.ThrowPredicate != null)
            {
                lazyAttempts = lazyAttempts.ThrowWhere(a => strategy.ThrowPredicate(a.Exception));
            }

            if (strategy.CatchPredicate != null)
            {
                lazyAttempts = lazyAttempts.CatchWhere(a => strategy.CatchPredicate(a.Exception));
            }

            return(lazyAttempts);
        }