Skip to content

Commit

Permalink
Add support for Request Timeouts to HTTP/2 Java 11 Client. (#1233)
Browse files Browse the repository at this point in the history
Adds `Request.Options` to the HTTP/2 Client Request Builder.
  • Loading branch information
ChunMengLu committed Jul 2, 2020
1 parent f9f294a commit ae514aa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
17 changes: 11 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,12 +53,13 @@ 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 {
httpResponse = client.send(httpRequest, BodyHandlers.ofByteArray());
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Invalid uri " + request.url(), e);
}

Expand All @@ -71,7 +76,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 +94,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 +158,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);
}

}
22 changes: 22 additions & 0 deletions java11/src/test/java/feign/http2client/test/Http2ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@

import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.Assertions;
import org.hamcrest.CoreMatchers;
import org.junit.Ignore;
import org.junit.Test;
import java.io.IOException;
import java.net.http.HttpTimeoutException;
import java.util.concurrent.TimeUnit;
import feign.*;
import feign.client.AbstractClientTest;
import feign.http2client.Http2Client;
Expand All @@ -37,6 +40,10 @@ public interface TestInterface {
@RequestLine("PATCH /patch")
@Headers({"Accept: text/plain"})
String patch();

@RequestLine("POST /timeout")
@Headers({"Accept: text/plain"})
String timeout();
}

@Override
Expand Down Expand Up @@ -80,6 +87,21 @@ public void testVeryLongResponseNullLength() {
// client is too smart to fall for a body that is 8 bytes long
}

@Test
public void timeoutTest() {
server.enqueue(new MockResponse().setBody("foo").setBodyDelay(30, TimeUnit.SECONDS));

final TestInterface api = newBuilder()
.retryer(Retryer.NEVER_RETRY)
.options(new Request.Options(1, TimeUnit.SECONDS, 1, TimeUnit.SECONDS, true))
.target(TestInterface.class, server.url("/").toString());

thrown.expect(FeignException.class);
thrown.expectCause(CoreMatchers.isA(HttpTimeoutException.class));

api.timeout();
}

@Override
public Feign.Builder newBuilder() {
return Feign.builder().client(new Http2Client());
Expand Down

1 comment on commit ae514aa

@zhangxinlong633
Copy link

Choose a reason for hiding this comment

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

666

Please sign in to comment.