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

Nima WebClient configuration for timeouts and keepAlive #6971

Merged
merged 2 commits into from
Jun 21, 2023
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
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 @@ -69,6 +71,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 All @@ -82,6 +85,21 @@ abstract class Builder<B extends Builder<B, C>, C extends WebClient> implements
protected Builder() {
}

/**
* 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 @@ -131,6 +149,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 @@ -140,6 +166,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 @@ -185,6 +222,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 @@ -200,7 +277,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 @@ -249,24 +326,32 @@ 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;
}
Expand All @@ -287,13 +372,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