c# – 在Cosmos DB中重试策略

我想了解如何最好地为cosmos db(documentdb)实现重试/退避策略.
据我所知,sdk中内置了一些认的重试机制,我可以在connectionpolicy中进行更改,如下所示:

RetryOptions = new RetryOptions() { MaxRetryAttemptsOnThrottledRequests = 3,MaxRetryWaitTimeInSeconds = 60 }

但是,我不确定这将如何影响我应该如何进行异常管理.

目前我正在做以下事情:

GetAsync<T>(Uri,Id) {

    try {

        ResourceResponse<Document> response = await client.ReadDocumentAsync(URiFactory.CreateDocumentUri(uri),new RequestOptions { PartitionKey = new PartitonKey(convert.ToInt64(id)) }).ConfigureAwait(false); 

    }
    catch(DocumentClientException ex) {
        if(ex.StatusCode == (HttpStatusCode)TooManyRequests) {
            await Task.Run(async () =>
            {
                await Task.Delay(ex.RetryAfter);
                return await GetAsync<T>(Uri,Id).ConfigureAwait(false);
            }
        }
    }
}

我需要重试吗?如果我捕获异常,是否会停止认重试尝试?此外,认重试尝试捕获的是什么?即它只是429?如果是这样我需要手动处理错误代码449?

解决方法

Custom RetryOptions仅用于处理限制(429错误代码).有关详细信息,请参阅 https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips#429.

在例外部分:API将仅在所有重试用尽异常后保释.

By default,the DocumentClientException with status code 429 is returned after a cumulative wait time of 30 seconds if the request continues to operate above the request rate. This occurs even when the current retry count is less than the max retry count,be it the default of 9 or a user-defined value.

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...