Reset Deflater/Inflater after Use in DeflateCompressor (#65617) (#65646)

We should reset after use, not before reuse. Otherwise we keep the input buffers
on these objects around for a long time and they can grow to O(MB).
This commit is contained in:
Armin Braun 2020-12-01 02:44:36 +01:00 committed by GitHub
parent bb0fcb150b
commit 6bbeedc932
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 3 deletions

View File

@ -229,9 +229,10 @@ public class DeflateCompressor implements Compressor {
public BytesReference uncompress(BytesReference bytesReference) throws IOException {
final BytesStreamOutput buffer = baos.get();
final Inflater inflater = inflaterRef.get();
inflater.reset();
try (InflaterOutputStream ios = new InflaterOutputStream(buffer, inflater)) {
bytesReference.slice(HEADER.length, bytesReference.length() - HEADER.length).writeTo(ios);
} finally {
inflater.reset();
}
final BytesReference res = buffer.copyBytes();
buffer.reset();
@ -245,11 +246,12 @@ public class DeflateCompressor implements Compressor {
@Override
public BytesReference compress(BytesReference bytesReference) throws IOException {
final BytesStreamOutput buffer = baos.get();
final Deflater deflater = deflaterRef.get();
deflater.reset();
buffer.write(HEADER);
final Deflater deflater = deflaterRef.get();
try (DeflaterOutputStream dos = new DeflaterOutputStream(buffer, deflater, true)) {
bytesReference.writeTo(dos);
} finally {
deflater.reset();
}
final BytesReference res = buffer.copyBytes();
buffer.reset();