Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional HttpClientOptions Configurations #22248

Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpClientProvider;
import com.azure.core.util.HttpClientOptions;
import reactor.netty.resources.ConnectionProvider;

/**
* An {@link HttpClientProvider} that provides an implementation of HttpClient based on Netty.
Expand All @@ -27,6 +28,18 @@ public HttpClient createInstance(HttpClientOptions clientOptions) {
.writeTimeout(clientOptions.getWriteTimeout())
.responseTimeout(clientOptions.getResponseTimeout())
.readTimeout(clientOptions.getReadTimeout());

int maximumConnectionPoolSize = clientOptions.getMaximumConnectionPoolSize();

ConnectionProvider.Builder connectionProviderBuilder = ConnectionProvider.builder("azure-sdk");
connectionProviderBuilder.maxIdleTime(clientOptions.getConnectionIdleTimeout());

// Only configure the maximum connections if it has been set in the options.
if (maximumConnectionPoolSize > 0) {
connectionProviderBuilder.maxConnections(maximumConnectionPoolSize);
}

builder = builder.connectionProvider(connectionProviderBuilder.build());
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
import com.azure.core.http.HttpClient;
import com.azure.core.util.HttpClientOptions;
import com.azure.core.http.HttpClientProvider;
import okhttp3.ConnectionPool;

import java.util.concurrent.TimeUnit;

/**
* An {@link HttpClientProvider} that provides an implementation of HttpClient based on OkHttp.
Expand All @@ -26,6 +29,15 @@ public HttpClient createInstance(HttpClientOptions clientOptions) {
.configuration(clientOptions.getConfiguration())
.writeTimeout(clientOptions.getWriteTimeout())
.readTimeout(clientOptions.getReadTimeout());

int maximumConnectionPoolSize = (clientOptions.getMaximumConnectionPoolSize() == 0)
? 5 // By default OkHttp uses a maximum idle connection count of 5.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not set anything if the user has not configured the pool size. This number could change with later releases of OkHttp.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a required parameter in the constructor, I'll check if they (OkHttp) have it as a constant anywhere.

: clientOptions.getMaximumConnectionPoolSize();

ConnectionPool connectionPool = new ConnectionPool(maximumConnectionPoolSize,
clientOptions.getConnectionIdleTimeout().toMillis(), TimeUnit.MILLISECONDS);

builder = builder.connectionPool(connectionPool);
}

return builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@
import com.azure.core.annotation.Fluent;
import com.azure.core.http.HttpClient;
import com.azure.core.http.ProxyOptions;
import com.azure.core.util.logging.ClientLogger;

import java.time.Duration;

/**
* General configuration options for {@link HttpClient HttpClients}.
* <p>
* {@link HttpClient} implementations may not support all configuration options in this class.
*/
@Fluent
public final class HttpClientOptions extends ClientOptions {
private static final Duration MINIMUM_TIMEOUT = Duration.ofMillis(1);
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(60);
private static final Duration NO_TIMEOUT = Duration.ZERO;

private final ClientLogger logger = new ClientLogger(HttpClientOptions.class);

private ProxyOptions proxyOptions;
private Configuration configuration;
private Duration writeTimeout;
private Duration responseTimeout;
private Duration readTimeout;
private int maximumConnectionPoolSize;
private Duration connectionIdleTimeout;

@Override
public HttpClientOptions setApplicationId(String applicationId) {
Expand Down Expand Up @@ -88,6 +95,8 @@ public Configuration getConfiguration() {
* If {@code writeTimeout} is {@code null} a 60 second timeout will be used, if it is a {@link Duration} less than
* or equal to zero then no write timeout will be applied. When applying the timeout the greater of one millisecond
* and the value of {@code writeTimeout} will be used.
* <p>
* By default the write timeout is 60 seconds.
*
* @param writeTimeout Write operation timeout duration.
* @return The updated HttpClientOptions object.
Expand All @@ -99,6 +108,8 @@ public HttpClientOptions setWriteTimeout(Duration writeTimeout) {

/**
* Gets the write timeout for a request to be sent.
* <p>
* By default the write timeout is 60 seconds.
*
* @return The write timeout of a request to be sent.
*/
Expand All @@ -115,6 +126,8 @@ public Duration getWriteTimeout() {
* If {@code responseTimeout} is {@code null} a 60 second timeout will be used, if it is a {@link Duration} less
* than or equal to zero then no timeout will be applied to the response. When applying the timeout the greater of
* one millisecond and the value of {@code responseTimeout} will be used.
* <p>
* By default the response timeout is 60 seconds.
*
* @param responseTimeout Response timeout duration.
* @return The updated HttpClientOptions object.
Expand All @@ -133,6 +146,8 @@ public HttpClientOptions responseTimeout(Duration responseTimeout) {
* If {@code responseTimeout} is {@code null} a 60 second timeout will be used, if it is a {@link Duration} less
* than or equal to zero then no timeout will be applied to the response. When applying the timeout the greater of
* one millisecond and the value of {@code responseTimeout} will be used.
* <p>
* By default the response timeout is 60 seconds.
*
* @param responseTimeout Response timeout duration.
* @return The updated HttpClientOptions object.
Expand All @@ -144,6 +159,8 @@ public HttpClientOptions setResponseTimeout(Duration responseTimeout) {

/**
* Gets the response timeout duration used when waiting for a server to reply.
* <p>
* By default the response timeout is 60 seconds.
*
* @return The response timeout duration.
*/
Expand All @@ -161,6 +178,8 @@ public Duration getResponseTimeout() {
* If {@code readTimeout} is {@code null} a 60 second timeout will be used, if it is a {@link Duration} less than or
* equal to zero then no timeout period will be applied to response read. When applying the timeout the greater of
* one millisecond and the value of {@code readTimeout} will be used.
* <p>
* By default the read timeout is 60 seconds.
*
* @param readTimeout Read timeout duration.
* @return The updated HttpClientOptions object.
Expand All @@ -180,6 +199,8 @@ public HttpClientOptions readTimeout(Duration readTimeout) {
* If {@code readTimeout} is {@code null} a 60 second timeout will be used, if it is a {@link Duration} less than or
* equal to zero then no timeout period will be applied to response read. When applying the timeout the greater of
* one millisecond and the value of {@code readTimeout} will be used.
* <p>
* By default the read timeout is 60 seconds.
*
* @param readTimeout Read timeout duration.
* @return The updated HttpClientOptions object.
Expand All @@ -191,13 +212,76 @@ public HttpClientOptions setReadTimeout(Duration readTimeout) {

/**
* Gets the read timeout duration used when reading the server response.
* <p>
* By default the read timeout is 60 seconds.
*
* @return The read timeout duration.
*/
public Duration getReadTimeout() {
return getTimeout(readTimeout);
}

/**
* Sets the maximum connection pool size used by the underlying HTTP client.
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
* <p>
* By default the maximum connection pool size is determined by the underlying HTTP client.
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
*
* @param maximumConnectionPoolSize The maximum connection pool size.
* @return The updated HttpClientOptions object.
* @throws IllegalArgumentException If {@code maximumConnectionPoolSize} is less than {@code 1}.
*/
public HttpClientOptions setMaximumConnectionPoolSize(int maximumConnectionPoolSize) {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
if (maximumConnectionPoolSize <= 0) {
throw logger.logExceptionAsError(
new IllegalArgumentException("'maximumConnectionPoolSize' cannot be less than 1."));
}

this.maximumConnectionPoolSize = maximumConnectionPoolSize;
return this;
}

/**
* Gets the maximum connection pool size used by the underlying HTTP client.
* <p>
* By default the maximum connection pool size is determined by the underlying HTTP client.
*
* @return The maximum connection pool size.
*/
public int getMaximumConnectionPoolSize() {
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
return maximumConnectionPoolSize;
}

/**
* Sets the duration of time before an idle connection.
* <p>
* The connection idle timeout begins once the connection has completed its last network request. Every time the
* connection is used the idle timeout will reset.
* <p>
* If {@code connectionIdleTimeout} is {@code null} a 60 second timeout will be used, if it is a {@link Duration}
* less than or equal to zero then no timeout period will be applied. When applying the timeout the greater of one
* millisecond and the value of {@code connectionIdleTimeout} will be used.
* <p>
* By default the connection idle timeout is 60 seconds.
*
* @param connectionIdleTimeout The connection idle timeout duration.
* @return The updated HttpClientOptions object.
*/
public HttpClientOptions setConnectionIdleTimeout(Duration connectionIdleTimeout) {
this.connectionIdleTimeout = connectionIdleTimeout;
return this;
}

/**
* Gets the duration of time before an idle connection is closed.
* <p>
* By default the connection idle timeout is 60 seconds.
*
* @return The connection idel timeout duration.
alzimmermsft marked this conversation as resolved.
Show resolved Hide resolved
*/
public Duration getConnectionIdleTimeout() {
return getTimeout(connectionIdleTimeout);
}

private static Duration getTimeout(Duration timeout) {
// Timeout is null, use the 60 second default.
if (timeout == null) {
Expand Down