Skip to content

Commit

Permalink
Nima WebClient configuration for timeouts and keepAlive (helidon-io#6971
Browse files Browse the repository at this point in the history
)

* Nima WebClient Shortcuts for timeouts
* WebClient config for keepAlive
* Implement build() in WebClient.Builder and introduce an abstract doBuild method.
This allows WebClient.Builder to run pre-build code to set the channel options from the default builder.

Slight re-org of WebClient.Builder:
 - move all package private methods at the bottom
 - add javadoc to protected methods

Update javadoc related to the default socket options builder.

---------

Signed-off-by: aserkes <andrii.serkes@oracle.com>
Co-authored-by: Romain Grecourt <romain.grecourt@oracle.com>
  • Loading branch information
2 people authored and arjav-desai committed Jun 22, 2023
1 parent a24abba commit 9d26eb2
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public Http2ClientBuilder prefetch(int prefetch) {
}

@Override
public Http2Client build() {
public Http2Client doBuild() {
return new Http2ClientImpl(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package io.helidon.nima.webclient;

import java.net.URI;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -70,6 +72,7 @@ abstract class Builder<B extends Builder<B, C>, C extends WebClient> implements
private URI baseUri;
private Tls tls;
private SocketOptions channelOptions;
private final SocketOptions.Builder channelOptionsBuilder = SocketOptions.builder();
private DnsResolver dnsResolver;
private DnsAddressLookup dnsAddressLookup;
private boolean followRedirect;
Expand Down Expand Up @@ -103,6 +106,21 @@ public B config(Config config) {
return identity();
}

/**
* Actual {@link #build()} implementation for {@link WebClient} subclasses.
*
* @return new client
*/
protected abstract C doBuild();

@Override
public C build() {
if (channelOptions == null) {
channelOptions = channelOptionsBuilder.build();
}
return doBuild();
}

/**
* Base uri used by the client in all requests.
*
Expand Down Expand Up @@ -152,6 +170,14 @@ public B tls(Supplier<Tls> tls) {

/**
* Socket options for connections opened by this client.
* Note that using this method will trump the default {@link SocketOptions.Builder}.
* Thus, all methods that operate on the default {@link SocketOptions.Builder} are ineffective:
* <ul>
* <li>{@link #channelOptions(Consumer)} </li>
* <li>{@link #readTimeout(Duration)}</li>
* <li>{@link #connectTimeout(Duration)} (Duration)}</li>
* <li>{@link #keepAlive(boolean)}</li>
* </ul>
*
* @param channelOptions options
* @return updated builder
Expand All @@ -161,6 +187,17 @@ public B channelOptions(SocketOptions channelOptions) {
return identity();
}

/**
* Configure the socket options for connections opened by this client.
*
* @param consumer {@link SocketOptions.Builder} consumer
* @return updated builder
*/
public B channelOptions(Consumer<SocketOptions.Builder> consumer) {
consumer.accept(channelOptionsBuilder);
return identity();
}

/**
* DNS resolver to be used by this client.
*
Expand Down Expand Up @@ -206,6 +243,46 @@ public B maxRedirects(int maxRedirect) {
return identity();
}

/**
* Connect timeout.
* This method operates on the default socket options builder and provides a shortcut for
* {@link SocketOptions.Builder#connectTimeout(Duration)}.
*
* @param connectTimeout connect timeout
* @return updated builder
*/
public B connectTimeout(Duration connectTimeout) {
channelOptionsBuilder.connectTimeout(connectTimeout);
return identity();
}

/**
* Sets the socket read timeout.
* This method operates on the default socket options builder and provides a shortcut for
* {@link SocketOptions.Builder#readTimeout(Duration)}.
*
* @param readTimeout read timeout
* @return updated builder
*/
public B readTimeout(Duration readTimeout) {
channelOptionsBuilder.readTimeout(readTimeout);
return identity();
}

/**
* Configure socket keep alive.
* This method operates on the default socket options builder and provides a shortcut for
* {@link SocketOptions.Builder#socketKeepAlive(boolean)}.
*
* @param keepAlive keep alive
* @return updated builder
* @see java.net.StandardSocketOptions#SO_KEEPALIVE
*/
public B keepAlive(boolean keepAlive) {
channelOptionsBuilder.socketKeepAlive(keepAlive);
return identity();
}

/**
* Configure a custom header to be sent. Some headers cannot be modified.
*
Expand All @@ -221,7 +298,7 @@ public B header(Http.HeaderValue header) {
/**
* Set header with multiple values. Some headers cannot be modified.
*
* @param name header name
* @param name header name
* @param values header values
* @return updated builder instance
*/
Expand Down Expand Up @@ -283,33 +360,36 @@ public B mediaTypeParserMode(ParserMode mode) {
*
* @return socket options
*/
SocketOptions channelOptions() {
protected SocketOptions channelOptions() {
return channelOptions;
}

/**
* Configured TLS.
* Default headers to be used in every request.
*
* @return TLS if configured, null otherwise
* @return default headers
*/
Tls tls() {
return tls;
protected WritableHeaders<?> defaultHeaders() {
return defaultHeaders;
}

/**
* Base request uri.
* Media type parsing mode for HTTP {@code Content-Type} header.
*
* @return client request base uri
* @return media type parsing mode
*/
protected ParserMode mediaTypeParserMode() {
return this.mediaTypeParserMode;
}

Tls tls() {
return tls;
}

URI baseUri() {
return baseUri;
}

/**
* Config of this client.
*
* @return client config
*/
Config config() {
return config;
}
Expand All @@ -330,13 +410,5 @@ int maxRedirect() {
return maxRedirect;
}

protected WritableHeaders<?> defaultHeaders() {
return defaultHeaders;
}

protected ParserMode mediaTypeParserMode() {
return this.mediaTypeParserMode;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ private Http1ClientBuilder() {
}

@Override
public Http1Client build() {
public Http1Client doBuild() {
configBuilder.defaultHeaders(defaultHeaders());
if (mediaContextBuilder != null) {
configBuilder.mediaContext(mediaContextBuilder.fallback(configBuilder.mediaContext()).build());
}
configBuilder.socketOptions(super.channelOptions());
return new Http1ClientImpl(configBuilder.build());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private Builder() {
}

@Override
public WsClient build() {
public WsClient doBuild() {
// these headers cannot be modified by user
header(HEADER_UPGRADE_WS);
header(HEADER_WS_VERSION);
Expand Down

0 comments on commit 9d26eb2

Please sign in to comment.