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 2 commits 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 @@ -43,14 +43,15 @@
import com.palantir.logsafe.UnsafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalStateException;
import com.palantir.logsafe.exceptions.SafeRuntimeException;
import com.palantir.logsafe.exceptions.SafeUncheckedIoException;
import feign.Request;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
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 +144,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 SafeRuntimeException("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 +236,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()) {
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