示例#1
0
        /// <summary>
        /// Execute a function in session pool.
        /// </summary>
        /// <typeparam name="T">The return type of the function.</typeparam>
        ///
        /// <param name="func">The function to be executed in the session pool. The operation can be cancelled.</param>
        /// <param name="retryPolicy">The policy on retry.</param>
        /// <param name="retryAction">The customer retry action. The operation can be cancelled.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        ///
        /// <returns>The result from the function.</returns>
        public async Task <T> Execute <T>(Func <TransactionExecutor, CancellationToken, Task <T> > func, RetryPolicy retryPolicy, Func <int, CancellationToken, Task> retryAction, CancellationToken cancellationToken = default)
        {
            QldbSession session = null;

            try
            {
                session = await this.GetSession(cancellationToken);

                return(await this.retryHandler.RetriableExecute(
                           ct => session.Execute(func, ct),
                           retryPolicy,
                           async ct => session = await this.StartNewSession(ct),
                           async ct =>
                {
                    this.poolPermits.Release();
                    session = await this.GetSession(ct);
                },
                           retryAction,
                           cancellationToken));
            }
            finally
            {
                if (session != null)
                {
                    session.Release();
                }
            }
        }
        /// <summary>
        /// Execute a function in session pool.
        /// </summary>
        /// <typeparam name="T">The return type of the function.</typeparam>
        /// <param name="func">The function to be executed in the session pool.</param>
        /// <param name="retryPolicy">The policy on retry.</param>
        /// <param name="retryAction">The customer retry action.</param>
        /// <returns>The result from the function.</returns>
        public T Execute <T>(Func <TransactionExecutor, T> func, RetryPolicy retryPolicy, Action <int> retryAction)
        {
            QldbSession session = null;

            try
            {
                session = this.GetSession();
                return(this.retryHandler.RetriableExecute(
                           () => session.Execute(func),
                           retryPolicy,
                           () => session = this.StartNewSession(),
                           () =>
                {
                    this.poolPermits.Release();
                    session = this.GetSession();
                },
                           retryAction));
            }
            finally
            {
                if (session != null)
                {
                    session.Release();
                }
            }
        }