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.

点赞