Skip to content

Commit

Permalink
Drain the input stream if the remaining data is small
Browse files Browse the repository at this point in the history
  • Loading branch information
wendigo committed Mar 5, 2025
1 parent c6604bb commit 6ffc489
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@
import static java.lang.Math.clamp;
import static java.lang.Math.max;
import static java.util.Objects.requireNonNull;
import static software.amazon.awssdk.utils.IoUtils.drainInputStream;

final class S3InputStream
extends TrinoInputStream
{
private static final long DEFAULT_TCP_BUFFER_SIZE = 1024 * 8;
private static final int MAX_SKIP_BYTES = 1024 * 1024;

private final Location location;
Expand Down Expand Up @@ -242,8 +244,16 @@ private void closeStream()
}

try (var _ = in) {
in.abort();
in.release();
// According to the documentation: Abort will close the underlying connection, dropping all remaining data
// in the stream, and not leaving the connection open to be used for future requests. This can be more expensive
// than just reading remaining data.
if (length != null && length - streamPosition > DEFAULT_TCP_BUFFER_SIZE) {
in.abort();
in.release();
}
else {
drainInputStream(in);
}
}
catch (AbortedException | IOException _) {
}
Expand Down

0 comments on commit 6ffc489

Please sign in to comment.