Some API methods perform complex tasks that can take more than a few seconds to
complete. These requests can take longer than the defaults allow. This can cause
errors on the API or client side. These are ways to avert issues caused by
extended request latency.

## Increase default timeout in client library

Some methods commonly respond with high latency. This is noted on the method's
reference page. Other methods may also respond with high latency on rare
occasions.

Some client libraries have default timeout limits. These limits cause errors
when requests have high latency. Default timeouts for some of the supported
client libraries are:

- **Java**: 20 seconds.
- **Python**: 60 seconds.
- **PHP**: 60 seconds.

Increase these default limits to avoid client-side errors. Here's how to change
the default timeout for your client library:

> [!CAUTION]
> **Caution:** The Display \& Video 360 API server timeout is 180 seconds. If a request takes longer than 180 seconds, the API will return an error. This limit can't be changed.

### 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](https://getcomposer.org/).**

       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 may exceed the server timeout of 180 seconds. In this
case, the API returns a `408` or `504` error.

If a request responds with one of these codes, retry the request using
[exponential backoff](https://developers.google.com/display-video/api/guides/concepts/optimization/quota#use_exponential_backoff).

If the error persists, [reach out to API support](https://developers.google.com/display-video/api/support/contact).