-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
85 changes: 85 additions & 0 deletions
85
...rc/main/java/software/amazon/awssdk/http/crt/internal/response/ResponseHandlerHelper.java
This file contains hidden or 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,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 releaseConnection(HttpStream stream) { | ||
if (connectionClosed.compareAndSet(false, true)) { | ||
connection.close(); | ||
stream.close(); | ||
} | ||
} | ||
|
||
/** | ||
* Close the connection completely | ||
*/ | ||
public void closeConnection(HttpStream stream) { | ||
if (connectionClosed.compareAndSet(false, true)) { | ||
connection.shutdown(); | ||
connection.close(); | ||
stream.close(); | ||
} | ||
} | ||
|
||
public void cleanUpConnectionBasedOnStatusCode(HttpStream stream) { | ||
// always close the connection on a 5XX response code. | ||
if (HttpStatusFamily.of(responseBuilder.statusCode()) == HttpStatusFamily.SERVER_ERROR) { | ||
closeConnection(stream); | ||
} else { | ||
releaseConnection(stream); | ||
} | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...test/java/software/amazon/awssdk/http/crt/internal/BaseHttpStreamResponseHandlerTest.java
This file contains hidden or 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,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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.