Skip to content

Optimize DialogueFeignClient small response reader #3114

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.StringReader;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
Expand Down Expand Up @@ -143,11 +143,7 @@ private static boolean includeRequestHeader(String headerName) {
}

private static String urlDecode(String input) {
try {
return URLDecoder.decode(input, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new SafeUncheckedIoException("Failed to decode path segment", e, UnsafeArg.of("encoded", input));
}
return URLDecoder.decode(input, StandardCharsets.UTF_8);
}

private static Optional<RequestBody> requestBody(Request request) {
Expand Down Expand Up @@ -239,6 +235,17 @@ public InputStream asInputStream() {

@Override
public Reader asReader() {
Integer maybeLength = length();
if (maybeLength != null && maybeLength < 8192) {
// Avoid InputStreamReader / HeapByteBuffer overhead for small (less than 8KiB) inputs,
// see https://github.com/FasterXML/jackson-core/pull/1081
try (InputStream inputStream = asInputStream()) {
Copy link
Preview

Copilot AI Aug 12, 2025

Choose a reason for hiding this comment

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

The try-with-resources block closes the InputStream after creating the StringReader, but the StringReader contains the full content and doesn't need the stream to remain open. However, this creates a potential issue if asInputStream() returns the same underlying stream instance that might be needed elsewhere. Consider ensuring this optimization only applies when it's safe to consume the entire stream.

Suggested change
try (InputStream inputStream = asInputStream()) {
InputStream inputStream = asInputStream();
try {

Copilot uses AI. Check for mistakes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

when the response is 8KiB or less we want to fully consume the response input stream and release the response after converting to an immutable String & StringReader for consumption by Jackson deserialization

return new StringReader(new String(inputStream.readAllBytes(), StandardCharsets.UTF_8));
} catch (IOException e) {
throw new SafeUncheckedIoException(
"Failed to read response body", e, SafeArg.of("length", maybeLength));
}
}
return new InputStreamReader(asInputStream(), StandardCharsets.UTF_8);
}

Expand Down