本文介绍了DocumentDB TransientFaultHandling最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为节流的DocumentDB客户端调用编写非常冗长的重试逻辑.

I've been writing very verbose retry logic for throttled DocumentDB client calls.

下面的示例是10次重试的常见示例.

The example below is a common example of this with 10 retry attempts.

我的问题有两个:这是最佳实践吗,还有没有那么冗长的方法来处理此问题?我看到有一个 Microsoft.Azure.Documents.Client.TransientFaultHandling nuget包应该可以用更少的代码来实现相同的结果,但是我在StackOverflow或Google上找不到任何示例,并且没有似乎是Microsoft提供的任何清晰的文档.

My question is two fold:Is this best practice, and is there a less verbose way to handle this? I see that there is a Microsoft.Azure.Documents.Client.TransientFaultHandling nuget package that is supposed to achieve the same results with less code, but I cannot find any examples on StackOverflow or Google and there does not seem to be any clear documentation available from Microsoft.

int maxRetryAttempts = 10;

while (maxRetryAttempts > 0)
{
    try
    {
        // Attempt to call DocumentDB Method
        // ---[DocumentDB Method Here]---
    }
    catch (DocumentClientException de)
    {
        if (de.StatusCode.HasValue)
        {
            var statusCode = (int)de.StatusCode;

            if (statusCode == 429 || statusCode == 503)
            {
                //Sleep for retry amount
                Thread.Sleep(de.RetryAfter);

                //Decrement max retry attempts 
                maxRetryAttempts--;
            }

        }
    }
    catch (AggregateException ae)
    {                    
        foreach (Exception ex in ae.InnerExceptions)
        {
            if (ex is DocumentClientException)
            {
                var documentClientException = ex as DocumentClientException;
                var statusCode = (int)documentClientException.StatusCode;
                if (statusCode == 429 || statusCode == 503)
                {
                    //Sleep for retry amount
                    Thread.Sleep(documentClientException.RetryAfter);

                    //Decrement max retry attempts 
                    maxRetryAttempts--;
                }
                else
                {
                    throw;
                }
            }
        }
    }
}

if (maxRetryAttempts < 0)
{
    //Max retry attempts reached
}

推荐答案

您可以使用Github存储库中用于文档数据库数据迁移工具的TransientFaultHandling Nuget包找到示例代码:

You can find sample code using the TransientFaultHandling Nuget package in the Github repo for the DocumentDB Data Migration Tool:

https://github.com/Azure/azure-documentdb-datamigrationtool/blob/master/DocumentDb/Microsoft.DataTransfer.DocumentDb.FunctionalTests/DocumentDbHelper.cs

看起来像这样:

private static IReliableReadWriteDocumentClient CreateClient(IDocumentDbConnectionSettings connectionSettings)
{
    return new DocumentClient(new Uri(connectionSettings.AccountEndpoint), connectionSettings.AccountKey)
        .AsReliable(new FixedInterval(10, TimeSpan.FromSeconds(1)));
}

这篇关于DocumentDB TransientFaultHandling最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 10:21