微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐