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

Java11client connect timeout #1307

Merged
merged 3 commits into from
Dec 23, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 41 additions & 1 deletion java11/src/main/java/feign/http2client/Http2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,24 @@ public Http2Client() {
.build());
}

public Http2Client(Options options) {
this(newClientBuilder(options)
.version(Version.HTTP_2)
.build());
}

public Http2Client(HttpClient client) {
this.client = Util.checkNotNull(client, "HttpClient must not be null");
}

@Override
public Response execute(Request request, Options options) throws IOException {
final HttpRequest httpRequest = newRequestBuilder(request, options).build();
HttpClient clientForRequest = getOrCreateClient(options);

HttpResponse<byte[]> httpResponse;
try {
httpResponse = client.send(httpRequest, BodyHandlers.ofByteArray());
httpResponse = clientForRequest.send(httpRequest, BodyHandlers.ofByteArray());
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Invalid uri " + request.url(), e);
Expand All @@ -76,6 +83,39 @@ public Response execute(Request request, Options options) throws IOException {
return response;
}

private HttpClient getOrCreateClient(Options options) {
if (doesClientConfigurationDiffer(options)) {
// create a new client from the existing one - but with connectTimeout and followRedirect
// settings from options
java.net.http.HttpClient.Builder builder = newClientBuilder(options)
.sslContext(client.sslContext())
.sslParameters(client.sslParameters())
.version(client.version());
client.authenticator().ifPresent(builder::authenticator);
client.cookieHandler().ifPresent(builder::cookieHandler);
client.executor().ifPresent(builder::executor);
client.proxy().ifPresent(builder::proxy);
return builder.build();
}
return client;
}

private boolean doesClientConfigurationDiffer(Options options) {
if ((client.followRedirects() == Redirect.ALWAYS) != options.isFollowRedirects()) {
return true;
}
return client.connectTimeout()
.map(timeout -> timeout.toMillis() != options.connectTimeoutMillis())
.orElse(true);
}

private static java.net.http.HttpClient.Builder newClientBuilder(Options options) {
return HttpClient
.newBuilder()
.followRedirects(options.isFollowRedirects() ? Redirect.ALWAYS : Redirect.NEVER)
.connectTimeout(Duration.ofMillis(options.connectTimeoutMillis()));
}

private Builder newRequestBuilder(Request request, Options options) throws IOException {
URI uri;
try {
Expand Down