Configure timeouts and retries

Many Display & Video 360 API methods perform complex operations that might take longer than a few seconds to complete. Requests to these methods can sometimes exceed expected latency, causing errors on the API or client side. This page lists practices for handling issues caused by extended request latency.

Increase default timeout in client library

A handful of specific methods have been labeled in their reference documentation as regularly exceeding expected latency. Other methods could also exhibit high-latency behavior periodically.

The default timeout limits for some client libraries could result in errors when making high-latency requests. Default timeouts for a subset of supported client libraries are:

  • Java: 20 seconds
  • Python: 60 seconds
  • PHP: 60 seconds

Client-side timeouts can be avoided by raising these default timeouts. Follow these instructions to adjust the default timeout for your client library during runtime:

Java

  1. Import necessary resources.

    import com.google.api.client.http.HttpRequest;
    import com.google.api.client.http.HttpRequestInitializer;
    import java.io.IOException;
    
  2. Build function for setting HTTP timeout.

    /**
     * Adjusts HTTP timeout values used by the provided request initializer.
     *
     * @param requestInitializer The {@link HttpRequestInitializer} used to authorize requests.
     * @param newHttpTimeout The HTTP timeout for requests in seconds.
     * @return An {@link HttpRequestInitializer} with modified HTTP timeout values.
     */
    private static HttpRequestInitializer setHttpTimeout(
        final HttpRequestInitializer requestInitializer,
        final int newHttpTimeout) {
      return new HttpRequestInitializer() {
        @Override
        public void initialize(HttpRequest httpRequest) throws IOException {
          requestInitializer.initialize(httpRequest);
          httpRequest.setConnectTimeout(newHttpTimeout * 1_000);
          httpRequest.setReadTimeout(newHttpTimeout * 1_000);
        }
      };
    }
    
  3. Call function when creating the Display & Video 360 API client.

    // Create authorized API client with non-default timeouts.
    DisplayVideo service =
        new DisplayVideo.Builder(
            credential.getTransport(),
            credential.getJsonFactory(),
            setHttpTimeout(credential, http-timeout-in-seconds)
        )
            .setApplicationName("displayvideo-java-installed-app-sample")
            .build();
    

Python

  1. Import Google API Python client library http module.

    from googleapiclient import http
    
  2. Update default timeout constant.

    http.DEFAULT_HTTP_TIMEOUT_SEC = http-timeout-in-seconds
    
  3. Build API service.

    # Build the API service.
    service = discovery.build(
      'displayvideo',
      'v3',
      discoveryServiceUrl=discovery_url,
      credentials=credentials)
    

PHP

  1. Download and install the Guzzle HTTP library using Composer.

    composer require guzzlehttp/guzzle:^7.0
    
  2. Create Guzzle HTTP client, assigning timeout value.

    $httpClient = new \GuzzleHttp\Client(['timeout' => http-timeout-in-seconds]);
    
  3. Create Google client and assign Guzzle HTTP client.

    $client = new Google_Client();
    $client->setHttpClient($httpClient);
    

Handle API timeout errors

In rare cases, requests completing complex operations could exceed the server-side timeout of 180 seconds, causing the API to return a 408 or 504 error response.

If a request responds with either of these error codes, we recommend that you retry these requests using the exponential backoff strategy.

If the error persists, reach out to support using the contact form.