Exemple #1
0
        /// <summary>
        /// Builds a <see cref="Policy{TResult}" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
        /// </summary>
        /// <typeparam name="TResult">The return type of delegates which may be executed through the policy.</typeparam>
        /// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
        /// <param name="timeoutStrategy">The timeout strategy.</param>
        /// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task{TResult}" /> capturing the abandoned, timed-out action.
        /// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
        /// <exception cref="System.ArgumentNullException">onTimeout</exception>
        public static TimeoutPolicy <TResult> Timeout <TResult>(Func <Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action <Context, TimeSpan, Task> onTimeout)
        {
            if (timeoutProvider == null)
            {
                throw new ArgumentNullException(nameof(timeoutProvider));
            }
            if (onTimeout == null)
            {
                throw new ArgumentNullException(nameof(onTimeout));
            }

            return(new TimeoutPolicy <TResult>(
                       (action, context, cancellationToken) => TimeoutEngine.Implementation <TResult>(
                           action,
                           context,
                           cancellationToken,
                           timeoutProvider,
                           timeoutStrategy,
                           onTimeout)
                       ));
        }
Exemple #2
0
        /// <summary>
        /// Builds a <see cref="Policy" /> that will wait for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
        /// </summary>
        /// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
        /// <param name="timeoutStrategy">The timeout strategy.</param>
        /// <param name="onTimeout">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task" /> capturing the abandoned, timed-out action.
        /// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
        /// <exception cref="System.ArgumentNullException">onTimeout</exception>
        public static TimeoutPolicy Timeout(Func <Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Action <Context, TimeSpan, Task> onTimeout)
        {
            if (timeoutProvider == null)
            {
                throw new ArgumentNullException(nameof(timeoutProvider));
            }
            if (onTimeout == null)
            {
                throw new ArgumentNullException(nameof(onTimeout));
            }

            return(new TimeoutPolicy(
                       (action, context, cancellationToken) => TimeoutEngine.Implementation(
                           (ctx, ct) => { action(ctx, ct); return EmptyStruct.Instance; },
                           context,
                           cancellationToken,
                           timeoutProvider,
                           timeoutStrategy,
                           onTimeout)
                       ));
        }
        /// <summary>
        /// Builds a <see cref="Policy" /> that will wait asynchronously for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException" /> will be thrown if the delegate does not complete within the configured timeout.
        /// </summary>
        /// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
        /// <param name="timeoutStrategy">The timeout strategy.</param>
        /// <param name="onTimeoutAsync">An action to call on timeout, passing the execution context, the timeout applied, the <see cref="Task" /> capturing the abandoned, timed-out action, and the captured <see cref="Exception"/>.
        /// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
        /// <exception cref="System.ArgumentNullException">onTimeoutAsync</exception>
        public static TimeoutPolicy TimeoutAsync(Func <Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy
                                                 , Func <Context, TimeSpan, Task, Exception, Task> onTimeoutAsync)
        {
            if (timeoutProvider == null)
            {
                throw new ArgumentNullException(nameof(timeoutProvider));
            }
            if (onTimeoutAsync == null)
            {
                throw new ArgumentNullException(nameof(onTimeoutAsync));
            }

            return(new TimeoutPolicy(
                       (action, context, cancellationToken, continueOnCapturedContext) => TimeoutEngine.ImplementationAsync(
                           async(ctx, ct) => { await action(ctx, ct).ConfigureAwait(continueOnCapturedContext); return EmptyStruct.Instance; },
                           context,
                           timeoutProvider,
                           timeoutStrategy,
                           onTimeoutAsync,
                           cancellationToken,
                           continueOnCapturedContext)
                       ));
        }
Exemple #4
0
        /// <summary>
        /// Builds a <see cref="Policy{TResult}"/> that will wait asynchronously for a delegate to complete for a specified period of time. A <see cref="TimeoutRejectedException"/> will be thrown if the delegate does not complete within the configured timeout.
        /// </summary>
        /// <param name="timeoutProvider">A function to provide the timeout for this execution.</param>
        /// <param name="timeoutStrategy">The timeout strategy.</param>
        /// <param name="onTimeoutAsync">An action to call on timeout, passing the execution context, the timeout applied, and a <see cref="Task"/> capturing the abandoned, timed-out action.
        /// <remarks>The Task parameter will be null if the executed action responded co-operatively to cancellation before the policy timed it out.</remarks></param>
        /// <returns>The policy instance.</returns>
        /// <exception cref="System.ArgumentNullException">timeoutProvider</exception>
        /// <exception cref="System.ArgumentNullException">onTimeoutAsync</exception>
        public static TimeoutPolicy <TResult> TimeoutAsync <TResult>(Func <Context, TimeSpan> timeoutProvider, TimeoutStrategy timeoutStrategy, Func <Context, TimeSpan, Task, Task> onTimeoutAsync)
        {
            if (timeoutProvider == null)
            {
                throw new ArgumentNullException(nameof(timeoutProvider));
            }
            if (onTimeoutAsync == null)
            {
                throw new ArgumentNullException(nameof(onTimeoutAsync));
            }

            return(new TimeoutPolicy <TResult>(
                       (action, context, cancellationToken, continueOnCapturedContext) => TimeoutEngine.ImplementationAsync(
                           action,
                           context,
                           timeoutProvider,
                           timeoutStrategy,
                           onTimeoutAsync,
                           cancellationToken,
                           continueOnCapturedContext)
                       ));
        }