DocumentDB TransientFaultHandling 最佳实践
DocumentDB TransientFaultHandling Best Practice
我一直在为受限的 DocumentDB 客户端调用编写非常冗长的重试逻辑。
下面的示例是重试 10 次的常见示例。
我的问题有两个:
这是最佳实践吗?有没有更简洁的方法来处理这个问题?我看到有一个 Microsoft.Azure.Documents.Client.TransientFaultHandling nuget 包应该用更少的代码实现相同的结果,但我在 Whosebug 或 Google 上找不到任何示例和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
}
您可以在 DocumentDB 数据迁移工具的 Github 存储库中找到使用 TransientFaultHandling Nuget 包的示例代码:
看起来像这样:
private static IReliableReadWriteDocumentClient CreateClient(IDocumentDbConnectionSettings connectionSettings)
{
return new DocumentClient(new Uri(connectionSettings.AccountEndpoint), connectionSettings.AccountKey)
.AsReliable(new FixedInterval(10, TimeSpan.FromSeconds(1)));
}
我一直在为受限的 DocumentDB 客户端调用编写非常冗长的重试逻辑。
下面的示例是重试 10 次的常见示例。
我的问题有两个: 这是最佳实践吗?有没有更简洁的方法来处理这个问题?我看到有一个 Microsoft.Azure.Documents.Client.TransientFaultHandling nuget 包应该用更少的代码实现相同的结果,但我在 Whosebug 或 Google 上找不到任何示例和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
}
您可以在 DocumentDB 数据迁移工具的 Github 存储库中找到使用 TransientFaultHandling Nuget 包的示例代码:
看起来像这样:
private static IReliableReadWriteDocumentClient CreateClient(IDocumentDbConnectionSettings connectionSettings)
{
return new DocumentClient(new Uri(connectionSettings.AccountEndpoint), connectionSettings.AccountKey)
.AsReliable(new FixedInterval(10, TimeSpan.FromSeconds(1)));
}