Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Logging C# Client Library API Retries?

BRH
Bronze 1
Bronze 1

I have a method implementing the GCP Storage API in .net 6 as follows:

 

 

byte[] imageDataBytes;
string bucketName = _configuration["UPLOADS_BUCKET"];
var storage = StorageClient.Create();
  using (var memoryStream = new MemoryStream())
  {
    try
    {
      await storage.DownloadObjectAsync(
        bucketName,
        GetImageDestination(application, environment, imageId, imageExt, width, height),
                        memoryStream);
    }
    catch (GoogleApiException e)
    {
      if (e.HttpStatusCode == HttpStatusCode.NotFound)
      {
        throw new ImageNotFoundException();
      }

      _logger.LogError(LogEvent.StorageFail, e, "Unhandled GoogleAPIException thrown.");
      throw;
    }

    imageDataBytes = memoryStream.ToArray();
  }

return imageDataBytes;

 

 

I have logged some (exceptionally infrequent) 503 errors when attempting to access files when GCP wasn't available. Looking into implementing a retry, documentation shows the C# client library implements an exponential backoff by default.  

Is there a way to log this backoff process and each GCP API retry?

Many thanks!

Solved Solved
0 4 829
1 ACCEPTED SOLUTION

Hi @BRH,

As of the moment we won't be able to check for the status to determine retries when using C#. I found this documentation wherein the author is using Python as not all exponential backoff logs are available in other languages. That's why I would like to request to file this one as a feature request however it's not a guarantee that this would be immediately available in future updates.

View solution in original post

4 REPLIES 4

Hi @BRH,

Welcome to Google Cloud Community!

As per documentation that you provided:

The C# client library uses exponential backoff by default.

However, it is also noted that currently, you cannot customize the default retry strategy used by the C# client library.

I would suggest to file this one as a feature request. It's not a guarantee that this would be added in future updates and we don't have a specific ETA on this. However, you can keep track of its progress once the ticket has been created.

Hi @robertcarlos,

Thanks for the reply!  

So to confirm, it seems like there is no logging or way to hook into a status to determine when retries are occurring? 

Hi @BRH,

As of the moment we won't be able to check for the status to determine retries when using C#. I found this documentation wherein the author is using Python as not all exponential backoff logs are available in other languages. That's why I would like to request to file this one as a feature request however it's not a guarantee that this would be immediately available in future updates.

Understood, thanks for the help! I'll create a ticket.