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

feat: java11 request options timeout. #1233

Merged
merged 4 commits into from
Jul 2, 2020
Merged
Changes from 1 commit
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
16 changes: 10 additions & 6 deletions java11/src/main/java/feign/http2client/Http2Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
*/
package feign.http2client;

import feign.Client;
import feign.Request;
import feign.Request.Options;
import feign.Response;
import feign.Util;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
Expand All @@ -26,11 +31,10 @@
import java.net.http.HttpRequest.Builder;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.time.Duration;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import feign.*;
import feign.Request.Options;

public class Http2Client implements Client {

Expand All @@ -49,7 +53,7 @@ public Http2Client(HttpClient client) {

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

HttpResponse<byte[]> httpResponse;
try {
Expand All @@ -71,7 +75,7 @@ public Response execute(Request request, Options options) throws IOException {
return response;
}

private Builder newRequestBuilder(Request request) throws IOException {
private Builder newRequestBuilder(Request request, Options options) throws IOException {
URI uri;
try {
uri = new URI(request.url());
Expand All @@ -89,6 +93,7 @@ private Builder newRequestBuilder(Request request) throws IOException {

final Builder requestBuilder = HttpRequest.newBuilder()
.uri(uri)
.timeout(Duration.ofMillis(options.readTimeoutMillis()))
.version(Version.HTTP_2);

final Map<String, Collection<String>> headers = filterRestrictedHeaders(request.headers());
Expand Down Expand Up @@ -152,8 +157,7 @@ private String[] asString(Map<String, Collection<String>> headers) {
.stream()
.map(value -> Arrays.asList(entry.getKey(), value))
.flatMap(List::stream))
.collect(Collectors.toList())
.toArray(new String[0]);
.toArray(String[]::new);
}

}