Skip to content

Commit b7eff6e

Browse files
authored
Best Practices for Handling Input Streams of responseBody in a Custom Client Using SdkHttpClient (#4624)
1 parent 8f6a1a2 commit b7eff6e

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

docs/BestPractices.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ This is because the input stream is a direct stream of data from the HTTP connec
2121
reused until all data from the stream has been read and the stream is closed. If these rules are not followed, the client can
2222
run out of resources by allocating too many open, but unused, HTTP connections.
2323

24+
### Close input streams of responseBody() from Custom Client based on SdkHttpClient
25+
While implementing custom clients from `SdkHttpClient` interface
26+
- Close the [responseBody](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/http/HttpExecuteResponse.html#responseBody()) Input Stream: Always close the "responseBody" input stream to release the HTTP Connection, even when handling error responses.
27+
- SDK Requirement: The SDK requires the closing of the input stream to manage HTTP connections effectively.
28+
- Handling Error Responses: Even in the case of error responses, the SDK client creates an input stream for reading error data.
29+
- Consistent Practice: Ensure that consumers close the input stream in the "responseBody" attribute for both success and error cases.
30+
2431
### Tune HTTP configurations based on performance tests
2532

2633
The SDK provides a set of [default http configurations] that apply to general use cases. Customers are recommended to tune the configurations for their applications based on their use cases.

http-client-spi/src/main/java/software/amazon/awssdk/http/HttpExecuteResponse.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ public SdkHttpResponse httpResponse() {
3838
}
3939

4040
/**
41-
* @return The {@link ContentStreamProvider}.
41+
* /** Get the {@link AbortableInputStream} associated with this response.
42+
*
43+
* <p>Always close the "responseBody" input stream to release the underlying HTTP connection.
44+
* Even for error responses, the SDK creates an input stream for reading error data. It is essential to close the input stream
45+
* in the "responseBody" attribute for both success and error cases.
46+
*
47+
* @return An {@link Optional} containing the {@link AbortableInputStream} if available.
4248
*/
4349
public Optional<AbortableInputStream> responseBody() {
4450
return responseBody;

0 commit comments

Comments
 (0)