Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions sdk/src/main/java/io/dapr/client/DaprHttpBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
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.
Expand All @@ -33,6 +36,13 @@ public class DaprHttpBuilder {
*/
private static final Object LOCK = new Object();

/**
* HTTP keep alive duration in seconds.
*
* <p>Just hard code to a reasonable value.
*/
private static final int KEEP_ALIVE_DURATION = 30;
Comment thread
skyao marked this conversation as resolved.

/**
* Build an instance of the Http client based on the provided setup.
*
Expand All @@ -55,6 +65,19 @@ private DaprHttp buildDaprHttp() {
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();
}
}
Expand Down
37 changes: 35 additions & 2 deletions sdk/src/main/java/io/dapr/config/Properties.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>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.
*
* <p>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.
Expand Down Expand Up @@ -123,5 +140,21 @@ public class Properties {
public static final Property<Integer> 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<Integer> 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<Integer> HTTP_CLIENT_MAX_IDLE_CONNECTIONS = new IntegerProperty(
"dapr.http.client.maxIdleConnections",
"DAPR_HTTP_CLIENT_MAX_IDLE_CONNECTIONS",
DEFAULT_HTTP_CLIENT_MAX_IDLE_CONNECTIONS);
}