Search code examples
.netazure-cosmosdb

Correct way to handle CosmosDb FeedResponse<T> from a feed iterator?


What is the correct way to handle CosmosDb FeedResponse from a feed iterator?

The documentation from the dotnet sdk has this example:

QueryDefinition queryDefinition = new QueryDefinition("select c.id From c where c.status = @status")
              .WithParameter("@status", "Failure");
using (FeedIterator<MyItem> feedIterator = this.Container.GetItemQueryIterator<MyItem>(
    queryDefinition))
{
    while (feedIterator.HasMoreResults)
    {
        FeedResponse<MyItem> response = await feedIterator.ReadNextAsync();
        foreach (var item in response)
        {
            Console.WriteLine(item);
        }
    }
}

But the example does not take the response.StatusCode and it doesn't seem like the feedITerator.ReadNextAsync() will throw any exceptions according to the documentation.

So I would assume that I should take the StatusCode into consideration when getting a feed iterator response. It could for example return 500 if the connection to the DB is bad or 429 if I used the allocated read units. There are probably many other cases where the status code would not be 200.

Any suggestions or references to some documentation that shows hot to handle the response?


Solution

  • Adding your own logic should not be necessary. The SDK has a retry policy by default that will automatically retry known transient errors.

    If you want to fine-tune it you can use the CosmosClientOptions and adjust the properties such as the MaxRetryAttemptsOnRateLimitedRequests and MaxRetryWaitTimeOnRateLimitedRequests should the default values not suit your case.