csharp Azure DocumentDB .Net SDK执行Async方法的示例,重试以处理RequestRateTooLargeException或HTTP 429错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp Azure DocumentDB .Net SDK执行Async方法的示例,重试以处理RequestRateTooLargeException或HTTP 429错误相关的知识,希望对你有一定的参考价值。

/// <summary>
/// Execute the function with retries on throttle
/// </summary>
/// <typeparam name="V"></typeparam>
/// <param name="client"></param>
/// <param name="function"></param>
/// <returns></returns>
private static async Task<V> ExecuteWithRetries<V>(DocumentClient client, Func<Task<V>> function)
{
        TimeSpan sleepTime = TimeSpan.Zero;

        while (true)
        {
                try
                {
                    return await function();
                }
                catch (DocumentClientException de)
                {
                    if ((int)de.StatusCode != 429)
                    {
                        throw;
                    }
                    sleepTime = de.RetryAfter;
                }
                catch (AggregateException ae)
                {
                    if (!(ae.InnerException is DocumentClientException))
                    {
                        throw;
                    }

                    DocumentClientException de = (DocumentClientException)ae.InnerException;
                    if ((int)de.StatusCode != 429)
                    {
                        throw;
                    }
                    sleepTime = de.RetryAfter;
                }

                await Task.Delay(sleepTime);
        }
}
        
/// <summary>
/// Async method for inserting a single document, with retries
/// </summary>
/// <param name="student"></param>
/// <returns></returns>
private async Task InsertDocumentAsync(Student student)
{

        ResourceResponse<Document> response = await ExecuteWithRetries(client, () => client.CreateDocumentAsync(colSelfLink, student));
            
}

以上是关于csharp Azure DocumentDB .Net SDK执行Async方法的示例,重试以处理RequestRateTooLargeException或HTTP 429错误的主要内容,如果未能解决你的问题,请参考以下文章

上手DocumentDB On Azure

使用 Java SDK v2 com.microsoft.azure.documentdb 的 Azure Cosmos 自动缩放

上手DocumentDB On Azure

通过php的MongoDB driver连接Azure的DocumentDB PaaS

创建 Azure 数据工厂管道以将新记录从 DocumentDB 复制到 Azure SQL

csharp DocumentDb