From e3fa24f67b8a8644d5bbf5e5e8ee51e0f02f8b75 Mon Sep 17 00:00:00 2001 From: Sky Ao Date: Mon, 21 Mar 2022 12:00:01 +0800 Subject: [PATCH] set maxRequestsPerHost of okhttp to support slow response requests and high TPS (#708) * set maxRequestsPerHost of okjava to support slow response requests and high TPS Signed-off-by: Sky Ao * fix typo in comments Signed-off-by: Sky Ao * update comments Signed-off-by: Sky Ao * update javadoc Signed-off-by: Sky Ao --- .../java/io/dapr/client/DaprHttpBuilder.java | 23 ++++++++++++ .../main/java/io/dapr/config/Properties.java | 37 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java b/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java index 5f71b0f7d..94b8f0cda 100644 --- a/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java +++ b/sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java @@ -14,9 +14,12 @@ limitations under the License. package io.dapr.client; import io.dapr.config.Properties; +import okhttp3.ConnectionPool; +import okhttp3.Dispatcher; import okhttp3.OkHttpClient; import java.time.Duration; +import java.util.concurrent.TimeUnit; /** * A builder for the DaprHttp. @@ -33,6 +36,13 @@ public class DaprHttpBuilder { */ private static final Object LOCK = new Object(); + /** + * HTTP keep alive duration in seconds. + * + *

Just hard code to a reasonable value. + */ + private static final int KEEP_ALIVE_DURATION = 30; + /** * Build an instance of the Http client based on the provided setup. * @@ -55,6 +65,19 @@ public class DaprHttpBuilder { OkHttpClient.Builder builder = new OkHttpClient.Builder(); Duration readTimeout = Duration.ofSeconds(Properties.HTTP_CLIENT_READ_TIMEOUT_SECONDS.get()); builder.readTimeout(readTimeout); + + Dispatcher dispatcher = new Dispatcher(); + dispatcher.setMaxRequests(Properties.HTTP_CLIENT_MAX_REQUESTS.get()); + // The maximum number of requests for each host to execute concurrently. + // Default value is 5 in okhttp which is totally UNACCEPTABLE! + // For sidecar case, set it the same as maxRequests. + dispatcher.setMaxRequestsPerHost(Properties.HTTP_CLIENT_MAX_REQUESTS.get()); + builder.dispatcher(dispatcher); + + ConnectionPool pool = new ConnectionPool(Properties.HTTP_CLIENT_MAX_IDLE_CONNECTIONS.get(), + KEEP_ALIVE_DURATION, TimeUnit.SECONDS); + builder.connectionPool(pool); + OK_HTTP_CLIENT = builder.build(); } } diff --git a/sdk/src/main/java/io/dapr/config/Properties.java b/sdk/src/main/java/io/dapr/config/Properties.java index 53b8720ec..fcd09d9b6 100644 --- a/sdk/src/main/java/io/dapr/config/Properties.java +++ b/sdk/src/main/java/io/dapr/config/Properties.java @@ -56,7 +56,24 @@ public class Properties { /** * Dapr's default timeout in seconds for HTTP client reads. */ - private static final Integer DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS = 60; + private static final Integer DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS = 60; + + /** + * Dapr's default maximum number of requests for HTTP client to execute concurrently. + * + *

Above this requests queue in memory, waiting for the running calls to complete. + * Default is 64 in okhttp which is OK for most case, but for some special case + * which is slow response and high concurrency, the value should set to a little big. + */ + private static final Integer DEFAULT_HTTP_CLIENT_MAX_REQUESTS = 1024; + + /** + * Dapr's default maximum number of idle connections of HTTP connection pool. + * + *

Attention! This is max IDLE connection, NOT max connection! + * It is also very important for high concurrency cases. + */ + private static final Integer DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS = 128; /** * IP for Dapr's sidecar. @@ -123,5 +140,21 @@ public class Properties { public static final Property HTTP_CLIENT_READ_TIMEOUT_SECONDS = new IntegerProperty( "dapr.http.client.readTimeoutSeconds", "DAPR_HTTP_CLIENT_READ_TIMEOUT_SECONDS", - DEFAULT_HTTP_CLIENT_READTIMEOUTSECONDS); + DEFAULT_HTTP_CLIENT_READ_TIMEOUT_SECONDS); + + /** + * Dapr's default maximum number of requests for HTTP client to execute concurrently. + */ + public static final Property HTTP_CLIENT_MAX_REQUESTS = new IntegerProperty( + "dapr.http.client.maxRequests", + "DAPR_HTTP_CLIENT_MAX_REQUESTS", + DEFAULT_HTTP_CLIENT_MAX_REQUESTS); + + /** + * Dapr's default maximum number of idle connections for HTTP connection pool. + */ + public static final Property HTTP_CLIENT_MAX_IDLE_CONNECTIONS = new IntegerProperty( + "dapr.http.client.maxIdleConnections", + "DAPR_HTTP_CLIENT_MAX_IDLE_CONNECTIONS", + DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS); }