1

Following an example in Google Sentiment Analysis Java API I have an application sending few hundred text samples for analysis to the service. Every now and then the API slows down significantly and when correlated with the timeline on the API Console, it is related to 500 and 504 errors.

Is there a recommended pattern to handle this kind of errors? I could not find a timeout parameter in the API, to be able to cancel the request say after 5 seconds and retry. Do I have to resort to wrapping the API call into a Future?

Adrián
  • 2,876
  • 17
  • 22
Altair7852
  • 1,226
  • 1
  • 14
  • 23

2 Answers2

1

Here is the exact API I was looking for.

The documentation:

And a code snippet:

    LanguageServiceClient createLanguage() throws IOException {
      RetrySettings retrySettings = RetrySettings.newBuilder()
            .setTotalTimeout(Duration.of(60, ChronoUnit.SECONDS))
            .setMaxAttempts(2)
            // Some other options
            .build();
      LanguageServiceSettings.Builder sb = 
            LanguageServiceSettings.newBuilder();
      sb.annotateTextSettings().setRetrySettings(retrySettings);
      return LanguageServiceClient.create(sb.build());
    }
Altair7852
  • 1,226
  • 1
  • 14
  • 23
0

Truncated exponential backoff may help you to handle this kind of errors.

Here are two links from google cloud platform, the first one explains the circumstance where you want to use it, and the second one gives you some examples.

https://cloud.google.com/apis/design/errors#error_retries https://cloud.google.com/storage/docs/exponential-backoff

If that didn’t solve your issue, you may check if your api requests hit the quota. In this case, you may apply for quota increase.

The quota for NLP API is 600 requests per minute (60 seconds): https://console.cloud.google.com/apis/api/language.googleapis.com/quotas

Or 1000 Requests per 100 seconds, which seems to be more or less the same: https://cloud.google.com/natural-language/quotas#requests

Xiaoxia Lin
  • 736
  • 6
  • 16