Skip to content

Commit 0bef9fd

Browse files
authored
Reuse connections for 5xx errors (#5607)
* Reuse connections for 5xx errors for HTTP/1 requests * Reuse HTTP/2 connections that receive a 5xx error * Fix flaky tests
1 parent 21c9655 commit 0bef9fd

File tree

16 files changed

+33
-106
lines changed

16 files changed

+33
-106
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS CRT HTTP Client",
4+
"contributor": "",
5+
"description": "Reuse connections that receive a 5xx service response."
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "Netty NIO HTTP Client",
4+
"contributor": "",
5+
"description": "Reuse connections that receive a 5xx service response."
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "Apache HTTP Client",
4+
"contributor": "",
5+
"description": "Reuse connections that receive a 5xx service response."
6+
}

http-clients/apache-client/src/main/java/software/amazon/awssdk/http/apache/ApacheHttpClient.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import software.amazon.awssdk.http.TlsTrustManagersProvider;
7676
import software.amazon.awssdk.http.apache.internal.ApacheHttpRequestConfig;
7777
import software.amazon.awssdk.http.apache.internal.DefaultConfiguration;
78-
import software.amazon.awssdk.http.apache.internal.SdkConnectionReuseStrategy;
7978
import software.amazon.awssdk.http.apache.internal.SdkProxyRoutePlanner;
8079
import software.amazon.awssdk.http.apache.internal.conn.ClientConnectionManagerFactory;
8180
import software.amazon.awssdk.http.apache.internal.conn.IdleConnectionReaper;
@@ -157,7 +156,6 @@ private ConnectionManagerAwareHttpClient createClient(ApacheHttpClient.DefaultBu
157156
.disableRedirectHandling()
158157
.disableAutomaticRetries()
159158
.setUserAgent("") // SDK will set the user agent header in the pipeline. Don't let Apache waste time
160-
.setConnectionReuseStrategy(new SdkConnectionReuseStrategy())
161159
.setConnectionManager(ClientConnectionManagerFactory.wrap(cm));
162160

163161
addProxyConfig(builder, configuration);

http-clients/apache-client/src/main/java/software/amazon/awssdk/http/apache/internal/SdkConnectionReuseStrategy.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/response/CrtResponseAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private void onSuccessfulResponseComplete(HttpStream stream) {
129129
completionFuture.complete(null);
130130
});
131131

132-
responseHandlerHelper.cleanUpConnectionBasedOnStatusCode(stream);
132+
responseHandlerHelper.releaseConnection(stream);
133133
}
134134

135135
private void handlePublisherError(HttpStream stream, Throwable failure) {

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/response/InputStreamAdaptingHttpStreamResponseHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,6 @@ private void onSuccessfulResponseComplete(HttpStream stream) {
147147

148148
// requestCompletionFuture has been completed at this point, no need to notify the future
149149
simplePublisher.complete();
150-
responseHandlerHelper.cleanUpConnectionBasedOnStatusCode(stream);
150+
responseHandlerHelper.releaseConnection(stream);
151151
}
152152
}

http-clients/aws-crt-client/src/main/java/software/amazon/awssdk/http/crt/internal/response/ResponseHandlerHelper.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import software.amazon.awssdk.crt.http.HttpHeader;
2121
import software.amazon.awssdk.crt.http.HttpHeaderBlock;
2222
import software.amazon.awssdk.crt.http.HttpStream;
23-
import software.amazon.awssdk.http.HttpStatusFamily;
2423
import software.amazon.awssdk.http.SdkHttpResponse;
2524

2625
/**
@@ -88,13 +87,4 @@ public void closeConnection(HttpStream stream) {
8887
}
8988
}
9089
}
91-
92-
public void cleanUpConnectionBasedOnStatusCode(HttpStream stream) {
93-
// always close the connection on a 5XX response code.
94-
if (HttpStatusFamily.of(responseBuilder.statusCode()) == HttpStatusFamily.SERVER_ERROR) {
95-
closeConnection(stream);
96-
} else {
97-
releaseConnection(stream);
98-
}
99-
}
10090
}

http-clients/aws-crt-client/src/test/java/software/amazon/awssdk/http/crt/internal/BaseHttpStreamResponseHandlerTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ void serverError_shouldShutdownConnection() {
7777
responseHandler.onResponseHeadersDone(httpStream, 0);
7878
responseHandler.onResponseComplete(httpStream, 0);
7979
requestFuture.join();
80-
verify(crtConn).shutdown();
8180
verify(crtConn).close();
8281
verify(httpStream).close();
8382
}
@@ -121,7 +120,6 @@ void streamClosed_shouldNotIncreaseStreamWindow() throws InterruptedException {
121120

122121
responseHandler.onResponseComplete(httpStream, 0);
123122
requestFuture.join();
124-
verify(crtConn).shutdown();
125123
verify(crtConn).close();
126124
verify(httpStream).close();
127125
verify(httpStream, never()).incrementWindow(anyInt());

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/ResponseHandler.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
import org.reactivestreams.Subscriber;
5757
import org.reactivestreams.Subscription;
5858
import software.amazon.awssdk.annotations.SdkInternalApi;
59-
import software.amazon.awssdk.http.HttpStatusFamily;
6059
import software.amazon.awssdk.http.Protocol;
6160
import software.amazon.awssdk.http.SdkCancellationException;
6261
import software.amazon.awssdk.http.SdkHttpFullResponse;
@@ -96,7 +95,7 @@ protected void channelRead0(ChannelHandlerContext channelContext, HttpObject msg
9695
.build();
9796
channelContext.channel().attr(RESPONSE_STATUS_CODE).set(response.status().code());
9897
channelContext.channel().attr(RESPONSE_CONTENT_LENGTH).set(responseContentLength(response));
99-
channelContext.channel().attr(KEEP_ALIVE).set(shouldKeepAlive(response));
98+
channelContext.channel().attr(KEEP_ALIVE).set(HttpUtil.isKeepAlive(response));
10099
ChannelUtils.getAttribute(channelContext.channel(), CHANNEL_DIAGNOSTICS)
101100
.ifPresent(ChannelDiagnostics::incrementResponseCount);
102101
requestContext.handler().onHeaders(sdkResponse);
@@ -203,13 +202,6 @@ private static void finalizeResponse(RequestContext requestContext, ChannelHandl
203202
}
204203
}
205204

206-
private boolean shouldKeepAlive(HttpResponse response) {
207-
if (HttpStatusFamily.of(response.status().code()) == HttpStatusFamily.SERVER_ERROR) {
208-
return false;
209-
}
210-
return HttpUtil.isKeepAlive(response);
211-
}
212-
213205
@Override
214206
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
215207
RequestContext requestContext = ctx.channel().attr(REQUEST_CONTEXT_KEY).get();

0 commit comments

Comments
 (0)