-
Notifications
You must be signed in to change notification settings - Fork 910
Fixed the issue in AWS CRT HTTP clients where the connection is shut down unnecessarily #4825
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* 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.crt.internal.response; | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.crt.http.HttpClientConnection; | ||
import software.amazon.awssdk.crt.http.HttpHeader; | ||
import software.amazon.awssdk.crt.http.HttpHeaderBlock; | ||
import software.amazon.awssdk.crt.http.HttpStream; | ||
import software.amazon.awssdk.http.HttpStatusFamily; | ||
import software.amazon.awssdk.http.SdkHttpResponse; | ||
|
||
/** | ||
* This is the helper class that contains common logic shared between {@link CrtResponseAdapter} and | ||
* {@link InputStreamAdaptingHttpStreamResponseHandler}. | ||
* | ||
* CRT connection will only be closed, i.e., not reused, in one of the following conditions: | ||
* 1. 5xx server error OR | ||
* 2. It fails to read the response. | ||
*/ | ||
@SdkInternalApi | ||
public class ResponseHandlerHelper { | ||
|
||
private final SdkHttpResponse.Builder responseBuilder; | ||
private final HttpClientConnection connection; | ||
private AtomicBoolean connectionClosed = new AtomicBoolean(false); | ||
|
||
public ResponseHandlerHelper(SdkHttpResponse.Builder responseBuilder, HttpClientConnection connection) { | ||
this.responseBuilder = responseBuilder; | ||
this.connection = connection; | ||
} | ||
|
||
public void onResponseHeaders(HttpStream stream, int responseStatusCode, int headerType, HttpHeader[] nextHeaders) { | ||
if (headerType == HttpHeaderBlock.MAIN.getValue()) { | ||
for (HttpHeader h : nextHeaders) { | ||
responseBuilder.appendHeader(h.getName(), h.getValue()); | ||
} | ||
responseBuilder.statusCode(responseStatusCode); | ||
} | ||
} | ||
|
||
/** | ||
* Release the connection back to the pool so that it can be reused. | ||
*/ | ||
public void releaseCrtConnection(HttpStream stream) { | ||
if (connectionClosed.compareAndSet(false, true)) { | ||
connection.close(); | ||
stream.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Close the connection completely | ||
*/ | ||
public void closeCrtConnection(HttpStream stream) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to add another bool to the args for shutdown the connection or not, instead of have two different method, which feels more natural to me. Also, I would rename it to releaseCrtConnectionAndStream/releaseCrtHttpResources, since the passed-in stream also gets released. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually like having two methods with meaningful names because it makes it more readable IMO. I'll definitely forget what passing true to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I considered |
||
if (connectionClosed.compareAndSet(false, true)) { | ||
connection.shutdown(); | ||
connection.close(); | ||
stream.close(); | ||
} | ||
} | ||
|
||
public void cleanUpCrtConnectionBasedOnStatusCode(HttpStream stream) { | ||
// always close the connection on a 5XX response code. | ||
if (HttpStatusFamily.of(responseBuilder.statusCode()) == HttpStatusFamily.SERVER_ERROR) { | ||
closeCrtConnection(stream); | ||
} else { | ||
releaseCrtConnection(stream); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* 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.crt.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import software.amazon.awssdk.crt.http.HttpClientConnection; | ||
import software.amazon.awssdk.crt.http.HttpException; | ||
import software.amazon.awssdk.crt.http.HttpHeader; | ||
import software.amazon.awssdk.crt.http.HttpHeaderBlock; | ||
import software.amazon.awssdk.crt.http.HttpStream; | ||
import software.amazon.awssdk.crt.http.HttpStreamResponseHandler; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public abstract class BaseHttpStreamResponseHandlerTest { | ||
@Mock HttpClientConnection crtConn; | ||
CompletableFuture requestFuture; | ||
|
||
@Mock | ||
private HttpStream httpStream; | ||
|
||
private HttpStreamResponseHandler responseHandler; | ||
|
||
abstract HttpStreamResponseHandler responseHandler(); | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
requestFuture = new CompletableFuture<>(); | ||
responseHandler = responseHandler(); | ||
} | ||
|
||
@Test | ||
void serverError_shouldShutdownConnection() { | ||
HttpHeader[] httpHeaders = getHttpHeaders(); | ||
responseHandler.onResponseHeaders(httpStream, 500, HttpHeaderBlock.MAIN.getValue(), | ||
httpHeaders); | ||
|
||
responseHandler.onResponseHeadersDone(httpStream, 0); | ||
responseHandler.onResponseComplete(httpStream, 0); | ||
requestFuture.join(); | ||
verify(crtConn).shutdown(); | ||
verify(crtConn).close(); | ||
verify(httpStream).close(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = { 200, 400, 202, 403 }) | ||
void nonServerError_shouldNotShutdownConnection(int statusCode) { | ||
HttpHeader[] httpHeaders = getHttpHeaders(); | ||
responseHandler.onResponseHeaders(httpStream, statusCode, HttpHeaderBlock.MAIN.getValue(), | ||
httpHeaders); | ||
|
||
responseHandler.onResponseHeadersDone(httpStream, 0); | ||
responseHandler.onResponseComplete(httpStream, 0); | ||
|
||
requestFuture.join(); | ||
verify(crtConn, never()).shutdown(); | ||
verify(crtConn).close(); | ||
verify(httpStream).close(); | ||
} | ||
|
||
@Test | ||
void failedToGetResponse_shouldShutdownConnection() { | ||
HttpHeader[] httpHeaders = getHttpHeaders(); | ||
responseHandler.onResponseHeaders(httpStream, 200, HttpHeaderBlock.MAIN.getValue(), | ||
httpHeaders); | ||
|
||
responseHandler.onResponseComplete(httpStream, 1); | ||
assertThatThrownBy(() -> requestFuture.join()).hasRootCauseInstanceOf(HttpException.class); | ||
verify(crtConn).shutdown(); | ||
verify(crtConn).close(); | ||
verify(httpStream).close(); | ||
} | ||
|
||
private static HttpHeader[] getHttpHeaders() { | ||
HttpHeader[] httpHeaders = new HttpHeader[1]; | ||
httpHeaders[0] = new HttpHeader("Content-Length", "1"); | ||
return httpHeaders; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.