App Http Client Timeout Configuration

Default timeouts available via GsiHttpClientUtils

de.gsi.fcc.commons.common.utils.http.GsiHttpClientUtils
  • httpConnectTimeout
  • httpReadWriteTimeout
https://git.acc.gsi.de/fcc-commons/common-utils/src/commit/c00bfd5ccc8488f543fc188a777c2c754a3ddb35/src/main/java/de/gsi/fcc/commons/common/utils/http/GsiHttpClientUtils.java#L6

Values are defined in our configuration files:

https://git.acc.gsi.de/fcc-applications/common-config/src/commit/e2c20e9f269b33783aad333c1f9000b46b2ef033/default.properties#L5

Terminology

  • Connection timeout: time to wait until the other side answers our connection request on a low level. Connection timeout could mean there is a network problem or the request dropped along the way or the server system is completely unresponsive.
  • Read / Write timeout: time to keep the low level (socket) connection open even if no data is received / sent. This can depend highly on your application or performed action. A timeout could mean that the service is non responsive anymore or the service has too much load. But be aware it could also just mean that a request takes a long time to process.
    • You usually don't want to cancel requests just because they take too long to answer so these timeouts might need adjustment based on the use case. On the other hand you don't want to cause a congestion in case of a high load situation with inefficient requests. This way they could be detected and optimized.
    • In case of long running connections, like Server Sent Events (SSE) connections, the server should send a heart beat message to keep the connection alive or maybe disable the timeout if this is not applicable.

Timeout configuration examples

Java HttpURLConnection (URL Object)

Configured by central properties sun.net.client.defaultReadTimeout and sun.net.client.defaultConnectTimeout. You just have to make sure that our default.properties are loaded on application start before making any network calls. These config settings are already in place since a couple of releases.

If you are using the Java Fx GSI Application Base class this is done automatically for you. https://www-acc.gsi.de/wiki/Applications/AppHowToConfiguration

PropertyConfig.loadDefaultProperties();

Java HttpClient 11

HttpClient client = HttpClient.newBuilder() //
        .connectTimeout(GsiHttpClientUtils.httpConnectTimeout()) //
        .build();

HttpRequest request = HttpRequest.newBuilder() //
        .uri(URI.create("https://www.gsi.de/start/aktuelles")) //
        .timeout(GsiHttpClientUtils.httpReadWriteTimeout()) //
        .GET() //
        .build();

HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println(response.body().substring(0, 512));

Apache httpcomponents-client 4.5

RequestConfig.Builder requestConfigBuilder = RequestConfig.custom() //
        .setConnectTimeout(GsiHttpClientUtils.httpConnectTimeoutMs()) //
        .setConnectionRequestTimeout(GsiHttpClientUtils.httpConnectTimeoutMs()) //
        .setSocketTimeout(GsiHttpClientUtils.httpReadWriteTimeoutMs());

return HttpClients.custom() //
        .setDefaultRequestConfig(requestConfigBuilder.build()) //
        .build();

HttpGet httpGet = new HttpGet(url);
// httpGet.setConfig(requestConfig); // alternative to setting it centrally or to overwrite it
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
    System.out.println(EntityUtils.toString(response.getEntity()).substring(0, 512));
} finally {
    response.close();
    httpClient.close(); // on app/service shutdown
}
  • If not set: Timeouts: "Default: -1. A timeout value of zero is interpreted as an infinite timeout. A negative value is interpreted as undefined (system default if applicable)."
  • So the default is quite vague, the following seems reasonable The default read timeout is infinity on all platforms. "The default connect timeout is of the order of a minute, but it varies by platform." https://stackoverflow.com/q/60998421/405793

Spring RestTemplate

Depends on the used client, it defaults to Java HttpURLConnection, see above.

For details on how to configure one or the other HTTP Client: https://www-acc.gsi.de/wiki/Applications/AppHttpClientComparison2021#Spring_RestTemplate
  • If not set: Timeout: Depends on the used client
  • Note: Spring RestTemplate is in maintainance mode and not actively developed anymore by Spring

OkHttp 3.6.0 / 4.9.2

OkHttpClient client = new OkHttpClient.Builder() //
        .connectTimeout(GsiHttpClientUtils.httpConnectTimeoutMs(), TimeUnit.MILLISECONDS) //
        .readTimeout(GsiHttpClientUtils.httpReadWriteTimeoutMs(), TimeUnit.MILLISECONDS) //
        .writeTimeout(GsiHttpClientUtils.httpReadWriteTimeoutMs(), TimeUnit.MILLISECONDS) //
        .build();

Request request = new Request.Builder() //
        .url("https://www.gsi.de/start/aktuelles") //
        .build();

try (Response response = client.newCall(request).execute()) {
    System.out.println(response.body().string().substring(0, 512));
}
  • If not set: The default value for connect, read, write is 10 seconds

More information

AppHttpClientComparison2021

-- BenjaminPeter - 08 Nov 2021
Topic revision: r3 - 09 Nov 2021, BenjaminPeter
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback