-
Notifications
You must be signed in to change notification settings - Fork 882
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now it's possible to configure NettyNioAsyncHttpClient in order to use a
non blocking DNS resolver.
- Loading branch information
1 parent
f12ccc7
commit 8dc8fe6
Showing
13 changed files
with
502 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"category": "Netty NIO HTTP Client", | ||
"contributor": "martinKindall", | ||
"type": "bugfix", | ||
"description": "By default, Netty threads are blocked during dns resolution, namely InetAddress.getByName is used under the hood. Now, there's an option to configure the NettyNioAsyncHttpClient in order to use a non blocking dns resolution strategy." | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
192 changes: 192 additions & 0 deletions
192
...java/software/amazon/awssdk/http/nio/netty/NettyNioAsyncHttpClientNonBlockingDnsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.http.nio.netty; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.any; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.verify; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; | ||
import static java.util.Collections.singletonMap; | ||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; | ||
import static org.apache.commons.lang3.StringUtils.reverse; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.assertCanReceiveBasicRequest; | ||
import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.createProvider; | ||
import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.createRequest; | ||
import static software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClientTestUtils.makeSimpleRequest; | ||
|
||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
import org.assertj.core.api.Condition; | ||
import org.junit.AfterClass; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import software.amazon.awssdk.http.SdkHttpConfigurationOption; | ||
import software.amazon.awssdk.http.SdkHttpFullRequest; | ||
import software.amazon.awssdk.http.SdkHttpMethod; | ||
import software.amazon.awssdk.http.SdkHttpRequest; | ||
import software.amazon.awssdk.http.async.AsyncExecuteRequest; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
import software.amazon.awssdk.utils.AttributeMap; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class NettyNioAsyncHttpClientNonBlockingDnsTest { | ||
|
||
private final RecordingNetworkTrafficListener wiremockTrafficListener = new RecordingNetworkTrafficListener(); | ||
|
||
private static final SdkAsyncHttpClient client = NettyNioAsyncHttpClient.builder() | ||
.buildWithDefaults( | ||
AttributeMap.builder() | ||
.put(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, true) | ||
.put(SdkHttpConfigurationOption.USE_NONBLOCKING_DNS_RESOLVER, true) | ||
.build()); | ||
|
||
@Rule | ||
public WireMockRule mockServer = new WireMockRule(wireMockConfig() | ||
.dynamicPort() | ||
.dynamicHttpsPort() | ||
.networkTrafficListener(wiremockTrafficListener)); | ||
|
||
@Before | ||
public void methodSetup() { | ||
wiremockTrafficListener.reset(); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() throws Exception { | ||
client.close(); | ||
} | ||
|
||
@Test | ||
public void useNonBlockingDnsResolver_shouldHonor() { | ||
try (NettyNioAsyncHttpClient client = (NettyNioAsyncHttpClient) NettyNioAsyncHttpClient.builder() | ||
.build()) { | ||
assertThat(client.configuration().isNonBlockingResolver()).isEqualTo(false); | ||
} | ||
|
||
try (NettyNioAsyncHttpClient client = (NettyNioAsyncHttpClient) NettyNioAsyncHttpClient.builder() | ||
.useNonBlockingDnsResolver(false) | ||
.build()) { | ||
assertThat(client.configuration().isNonBlockingResolver()).isEqualTo(false); | ||
} | ||
|
||
try (NettyNioAsyncHttpClient client = (NettyNioAsyncHttpClient) NettyNioAsyncHttpClient.builder() | ||
.useNonBlockingDnsResolver(true) | ||
.build()) { | ||
assertThat(client.configuration().isNonBlockingResolver()).isEqualTo(true); | ||
} | ||
} | ||
|
||
@Test | ||
public void canSendContentAndGetThatContentBackNonBlockingDns() throws Exception { | ||
String body = randomAlphabetic(50); | ||
stubFor(any(urlEqualTo("/echo?reversed=true")) | ||
.withRequestBody(equalTo(body)) | ||
.willReturn(aResponse().withBody(reverse(body)))); | ||
URI uri = URI.create("http://localhost:" + mockServer.port()); | ||
|
||
SdkHttpRequest request = createRequest(uri, "/echo", body, SdkHttpMethod.POST, singletonMap("reversed", "true")); | ||
|
||
RecordingResponseHandler recorder = new RecordingResponseHandler(); | ||
|
||
client.execute(AsyncExecuteRequest.builder().request(request).requestContentPublisher(createProvider(body)).responseHandler(recorder).build()); | ||
|
||
recorder.completeFuture.get(5, TimeUnit.SECONDS); | ||
|
||
verify(1, postRequestedFor(urlEqualTo("/echo?reversed=true"))); | ||
|
||
assertThat(recorder.fullResponseAsString()).isEqualTo(reverse(body)); | ||
} | ||
|
||
@Test | ||
public void defaultThreadFactoryUsesHelpfulName() throws Exception { | ||
// Make a request to ensure a thread is primed | ||
makeSimpleRequest(client, mockServer); | ||
|
||
String expectedPattern = "aws-java-sdk-NettyEventLoop-\\d+-\\d+"; | ||
assertThat(Thread.getAllStackTraces().keySet()) | ||
.areAtLeast(1, new Condition<>(t -> t.getName().matches(expectedPattern), | ||
"Matches default thread pattern: `%s`", expectedPattern)); | ||
} | ||
|
||
@Test | ||
public void canMakeBasicRequestOverHttp() throws Exception { | ||
String smallBody = randomAlphabetic(10); | ||
URI uri = URI.create("http://localhost:" + mockServer.port()); | ||
|
||
assertCanReceiveBasicRequest(client, uri, smallBody); | ||
} | ||
|
||
@Test | ||
public void canMakeBasicRequestOverHttps() throws Exception { | ||
String smallBody = randomAlphabetic(10); | ||
URI uri = URI.create("https://localhost:" + mockServer.httpsPort()); | ||
|
||
assertCanReceiveBasicRequest(client, uri, smallBody); | ||
} | ||
|
||
@Test | ||
public void canHandleLargerPayloadsOverHttp() throws Exception { | ||
String largishBody = randomAlphabetic(25000); | ||
|
||
URI uri = URI.create("http://localhost:" + mockServer.port()); | ||
|
||
assertCanReceiveBasicRequest(client, uri, largishBody); | ||
} | ||
|
||
@Test | ||
public void canHandleLargerPayloadsOverHttps() throws Exception { | ||
String largishBody = randomAlphabetic(25000); | ||
|
||
URI uri = URI.create("https://localhost:" + mockServer.httpsPort()); | ||
|
||
assertCanReceiveBasicRequest(client, uri, largishBody); | ||
} | ||
|
||
@Test | ||
public void requestContentOnlyEqualToContentLengthHeaderFromProvider() throws InterruptedException, ExecutionException, TimeoutException, IOException { | ||
final String content = randomAlphabetic(32); | ||
final String streamContent = content + reverse(content); | ||
stubFor(any(urlEqualTo("/echo?reversed=true")) | ||
.withRequestBody(equalTo(content)) | ||
.willReturn(aResponse().withBody(reverse(content)))); | ||
URI uri = URI.create("http://localhost:" + mockServer.port()); | ||
|
||
SdkHttpFullRequest request = createRequest(uri, "/echo", streamContent, SdkHttpMethod.POST, singletonMap("reversed", "true")); | ||
request = request.toBuilder().putHeader("Content-Length", Integer.toString(content.length())).build(); | ||
RecordingResponseHandler recorder = new RecordingResponseHandler(); | ||
|
||
client.execute(AsyncExecuteRequest.builder().request(request).requestContentPublisher(createProvider(streamContent)).responseHandler(recorder).build()); | ||
|
||
recorder.completeFuture.get(5, TimeUnit.SECONDS); | ||
|
||
// HTTP servers will stop processing the request as soon as it reads | ||
// bytes equal to 'Content-Length' so we need to inspect the raw | ||
// traffic to ensure that there wasn't anything after that. | ||
assertThat(wiremockTrafficListener.requests().toString()).endsWith(content); | ||
} | ||
} |
Oops, something went wrong.