加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在Cosmos DB中重试策略

发布时间:2020-12-15 22:47:57 所属栏目:百科 来源:网络整理
导读:我想了解如何最好地为cosmos db(documentdb)实现重试/退避策略. 据我所知,sdk中内置了一些默认的重试机制,我可以在connectionpolicy中进行更改,如下所示: RetryOptions = new RetryOptions() { MaxRetryAttemptsOnThrottledRequests = 3,MaxRetryWaitTimeIn
我想了解如何最好地为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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读