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:
Java
Import necessary resources.
import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestInitializer; import java.io.IOException;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); } }; }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
Import Google API Python client library http module.
from googleapiclient import httpUpdate default timeout constant.
http.DEFAULT_HTTP_TIMEOUT_SEC = http-timeout-in-secondsBuild API service.
# Build the API service. service = discovery.build( 'displayvideo', 'v3', discoveryServiceUrl=discovery_url, credentials=credentials)
PHP
Download and install the Guzzle HTTP library using Composer.
composer require guzzlehttp/guzzle:^7.0Create Guzzle HTTP client, assigning timeout value.
$httpClient = new \GuzzleHttp\Client(['timeout' => http-timeout-in-seconds]);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.
If the error persists, reach out to API support.