Skip to content

Update Apache to not reuse connections if they received a 5xx error. #2960

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

Merged
merged 1 commit into from
Jan 12, 2022
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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-ApacheHTTPClient-414635a.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "Apache HTTP Client",
"contributor": "",
"type": "bugfix",
"description": "Do not reuse connections that receive a 5xx service response."
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
import software.amazon.awssdk.http.TlsTrustManagersProvider;
import software.amazon.awssdk.http.apache.internal.ApacheHttpRequestConfig;
import software.amazon.awssdk.http.apache.internal.DefaultConfiguration;
import software.amazon.awssdk.http.apache.internal.SdkConnectionReuseStrategy;
import software.amazon.awssdk.http.apache.internal.SdkProxyRoutePlanner;
import software.amazon.awssdk.http.apache.internal.conn.ClientConnectionManagerFactory;
import software.amazon.awssdk.http.apache.internal.conn.IdleConnectionReaper;
Expand Down Expand Up @@ -161,6 +162,7 @@ private ConnectionManagerAwareHttpClient createClient(ApacheHttpClient.DefaultBu
.disableRedirectHandling()
.disableAutomaticRetries()
.setUserAgent("") // SDK will set the user agent header in the pipeline. Don't let Apache waste time
.setConnectionReuseStrategy(new SdkConnectionReuseStrategy())
.setConnectionManager(ClientConnectionManagerFactory.wrap(cm));

addProxyConfig(builder, configuration);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.apache.internal;

import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultClientConnectionReuseStrategy;
import org.apache.http.protocol.HttpContext;
import software.amazon.awssdk.annotations.SdkInternalApi;

/**
* Do not reuse connections that returned a 5xx error.
*
* <p>This is not strictly the behavior we would want in an AWS client, because sometimes we might want to keep a connection open
* (e.g. an undocumented service's 503 'SlowDown') and sometimes we might want to close the connection (e.g. S3's 400
* RequestTimeout or Glacier's 408 RequestTimeoutException), but this is good enough for the majority of services, and the ones
* for which it is not should not be impacted too harshly.
*/
@SdkInternalApi
public class SdkConnectionReuseStrategy extends DefaultClientConnectionReuseStrategy {
@Override
public boolean keepAlive(HttpResponse response, HttpContext context) {
if (!super.keepAlive(response, context)) {
return false;
}

if (response == null || response.getStatusLine() == null) {
return false;
}

return !is500(response);
}

private boolean is500(HttpResponse httpResponse) {
return httpResponse.getStatusLine().getStatusCode() / 100 == 5;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ protected SdkHttpClient createSdkHttpClient(SdkHttpClientOptions options) {
return builder.buildWithDefaults(attributeMap.build());
}

@Override
public void connectionsAreNotReusedOn5xxErrors() {
// We cannot support this because the URL connection client doesn't allow us to disable connection reuse
}

@AfterEach
public void reset() {
HttpsURLConnection.setDefaultSSLSocketFactory((SSLSocketFactory) SSLSocketFactory.getDefault());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public void testTrustAllWorks() {
public void testCustomTlsTrustManagerAndTrustAllFails() {
}

@Disabled
@Override
public void connectionsAreNotReusedOn5xxErrors() throws Exception {
// We cannot support this because the URL connection client doesn't allow us to disable connection reuse
}

@Test
public void testGetResponseCodeNpeIsWrappedAsIo() throws Exception {
connectionInterceptor = safeFunction(connection -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,30 @@ public void connectionPoolingWorks() throws Exception {
assertThat(CONNECTION_COUNTER.openedConnections()).isEqualTo(initialOpenedConnections + 1);
}

@Test
public void connectionsAreNotReusedOn5xxErrors() throws Exception {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

int initialOpenedConnections = CONNECTION_COUNTER.openedConnections();

SdkHttpClientOptions httpClientOptions = new SdkHttpClientOptions();
httpClientOptions.trustAll(true);
SdkHttpClient client = createSdkHttpClient(httpClientOptions);

stubForMockRequest(503);

for (int i = 0; i < 5; i++) {
SdkHttpFullRequest req = mockSdkRequest("http://localhost:" + mockServer.port(), SdkHttpMethod.POST);
HttpExecuteResponse response =
client.prepareRequest(HttpExecuteRequest.builder()
.request(req)
.contentStreamProvider(req.contentStreamProvider().orElse(null))
.build())
.call();
response.responseBody().ifPresent(IoUtils::drainInputStream);
}

assertThat(CONNECTION_COUNTER.openedConnections()).isEqualTo(initialOpenedConnections + 5);
}

@Test
public void testCustomTlsTrustManager() throws Exception {
WireMockServer selfSignedServer = HttpTestUtils.createSelfSignedServer();
Expand Down