GeezerVison is showing github.com — JdkZlibDecoder: accumulate decompressed output before firing channelR… · netty/netty@a7fbb6f
File treeExpand file treeCollapse file tree Open diff view settings codec-compression/src/main/java/io/netty/handler/codec/compression
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/BrotliDecoder.java +23-3Lines changed: 23 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 32 | 32 | */ | 33 | 33 | public final class BrotliDecoder extends ByteToMessageDecoder { | 34 | 34 |
| | 35 | + private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES; | | 36 | + | 35 | 37 | private enum State { | 36 | 38 | DONE, NEEDS_MORE_INPUT, ERROR | 37 | 39 | } | @@ -48,6 +50,7 @@ private enum State { | 48 | 50 | private DecoderJNI.Wrapper decoder; | 49 | 51 | private boolean destroyed; | 50 | 52 | private boolean needsRead; | | 53 | + private ByteBuf accumBuffer; | 51 | 54 |
| 52 | 55 | /** | 53 | 56 | * Creates a new BrotliDecoder with a default 8kB input buffer | @@ -67,10 +70,25 @@ public BrotliDecoder(int inputBufferSize) { | 67 | 70 | private void forwardOutput(ChannelHandlerContext ctx) { | 68 | 71 | ByteBuffer nativeBuffer = decoder.pull(); | 69 | 72 | // nativeBuffer actually wraps brotli's internal buffer so we need to copy its content | 70 | | - ByteBuf copy = ctx.alloc().buffer(nativeBuffer.remaining()); | 71 | | - copy.writeBytes(nativeBuffer); | | 73 | + int remaining = nativeBuffer.remaining(); | | 74 | + if (accumBuffer == null) { | | 75 | + accumBuffer = ctx.alloc().buffer(remaining); | | 76 | + } | | 77 | + accumBuffer.writeBytes(nativeBuffer); | 72 | 78 | needsRead = false; | 73 | | - ctx.fireChannelRead(copy); | | 79 | + if (accumBuffer.readableBytes() >= DEFAULT_MAX_FORWARD_BYTES) { | | 80 | + ctx.fireChannelRead(accumBuffer); | | 81 | + accumBuffer = null; | | 82 | + } | | 83 | + } | | 84 | + | | 85 | + private void flushAccumBuffer(ChannelHandlerContext ctx) { | | 86 | + if (accumBuffer != null && accumBuffer.isReadable()) { | | 87 | + ctx.fireChannelRead(accumBuffer); | | 88 | + } else if (accumBuffer != null) { | | 89 | + accumBuffer.release(); | | 90 | + } | | 91 | + accumBuffer = null; | 74 | 92 | } | 75 | 93 |
| 76 | 94 | private State decompress(ChannelHandlerContext ctx, ByteBuf input) { | @@ -145,6 +163,8 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t | 145 | 163 | } catch (Exception e) { | 146 | 164 | destroy(); | 147 | 165 | throw e; | | 166 | + } finally { | | 167 | + flushAccumBuffer(ctx); | 148 | 168 | } | 149 | 169 | } | 150 | 170 |
| |
Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/CompressionUtil.java +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 16 | 16 | package io.netty.handler.codec.compression; | 17 | 17 |
| 18 | 18 | import io.netty.buffer.ByteBuf; | | 19 | +import io.netty.util.internal.SystemPropertyUtil; | 19 | 20 |
| 20 | 21 | import java.nio.ByteBuffer; | 21 | 22 |
| 22 | 23 | final class CompressionUtil { | 23 | 24 |
| | 25 | + static final int DEFAULT_MAX_FORWARD_BYTES = SystemPropertyUtil.getInt( | | 26 | + "io.netty.compression.defaultMaxForwardBytes", 64 * 1024); | | 27 | + | 24 | 28 | private CompressionUtil() { } | 25 | 29 |
| 26 | 30 | static void checkChecksum(ByteBufChecksum checksum, ByteBuf uncompressed, int currentChecksum) { | |
Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/JZlibDecoder.java +5-1Lines changed: 5 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,8 @@ public class JZlibDecoder extends ZlibDecoder { | 28 | 28 |
| 29 | 29 | private final Inflater z = new Inflater(); | 30 | 30 | private byte[] dictionary; | | 31 | + private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES; | | 32 | + private final int maxForwardBytes; | 31 | 33 | private boolean needsRead; | 32 | 34 | private volatile boolean finished; | 33 | 35 |
| @@ -78,6 +80,7 @@ public JZlibDecoder(ZlibWrapper wrapper) { | 78 | 80 | */ | 79 | 81 | public JZlibDecoder(ZlibWrapper wrapper, int maxAllocation) { | 80 | 82 | super(maxAllocation); | | 83 | + this.maxForwardBytes = maxAllocation > 0 ? maxAllocation : DEFAULT_MAX_FORWARD_BYTES; | 81 | 84 |
| 82 | 85 | ObjectUtil.checkNotNull(wrapper, "wrapper"); | 83 | 86 |
| @@ -113,6 +116,7 @@ public JZlibDecoder(byte[] dictionary) { | 113 | 116 | */ | 114 | 117 | public JZlibDecoder(byte[] dictionary, int maxAllocation) { | 115 | 118 | super(maxAllocation); | | 119 | + this.maxForwardBytes = maxAllocation > 0 ? maxAllocation : DEFAULT_MAX_FORWARD_BYTES; | 116 | 120 | this.dictionary = ObjectUtil.checkNotNull(dictionary, "dictionary"); | 117 | 121 | int resultCode; | 118 | 122 | resultCode = z.inflateInit(JZlib.W_ZLIB); | @@ -174,7 +178,7 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t | 174 | 178 | int outputLength = z.next_out_index - oldNextOutIndex; | 175 | 179 | if (outputLength > 0) { | 176 | 180 | decompressed.writerIndex(decompressed.writerIndex() + outputLength); | 177 | | - if (maxAllocation == 0) { | | 181 | + if (maxAllocation == 0 && decompressed.readableBytes() >= maxForwardBytes) { | 178 | 182 | // If we don't limit the maximum allocations we should just | 179 | 183 | // forward the buffer directly. | 180 | 184 | ByteBuf buffer = decompressed; | |
Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecoder.java +7-3Lines changed: 7 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,9 @@ private enum GzipState { | 59 | 59 | private int xlen = -1; | 60 | 60 | private boolean needsRead; | 61 | 61 |
| | 62 | + private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES; | | 63 | + private final int maxForwardBytes; | | 64 | + | 62 | 65 | private volatile boolean finished; | 63 | 66 |
| 64 | 67 | private boolean decideZlibOrNone; | @@ -161,6 +164,7 @@ public JdkZlibDecoder(boolean decompressConcatenated, int maxAllocation) { | 161 | 164 |
| 162 | 165 | private JdkZlibDecoder(ZlibWrapper wrapper, byte[] dictionary, boolean decompressConcatenated, int maxAllocation) { | 163 | 166 | super(maxAllocation); | | 167 | + this.maxForwardBytes = maxAllocation > 0 ? maxAllocation : DEFAULT_MAX_FORWARD_BYTES; | 164 | 168 |
| 165 | 169 | ObjectUtil.checkNotNull(wrapper, "wrapper"); | 166 | 170 |
| @@ -265,9 +269,9 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t | 265 | 269 | if (crc != null) { | 266 | 270 | crc.update(outArray, outIndex, outputLength); | 267 | 271 | } | 268 | | - if (maxAllocation == 0) { | 269 | | - // If we don't limit the maximum allocations we should just | 270 | | - // forward the buffer directly. | | 272 | + if (maxAllocation == 0 && decompressed.readableBytes() >= maxForwardBytes) { | | 273 | + // Forward the buffer once it exceeds the threshold to bound memory | | 274 | + // while avoiding excessive fireChannelRead calls. | 271 | 275 | ByteBuf buffer = decompressed; | 272 | 276 | decompressed = null; | 273 | 277 | needsRead = false; | |
Collapse file Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/ZstdDecoder.java +12-3Lines changed: 12 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -41,7 +41,10 @@ public final class ZstdDecoder extends ByteToMessageDecoder { | 41 | 41 | } | 42 | 42 | } | 43 | 43 |
| | 44 | + private static final int DEFAULT_MAX_FORWARD_BYTES = CompressionUtil.DEFAULT_MAX_FORWARD_BYTES; | | 45 | + | 44 | 46 | private final int maximumAllocationSize; | | 47 | + private final int maxForwardBytes; | 45 | 48 | private final MutableByteBufInputStream inputStream = new MutableByteBufInputStream(); | 46 | 49 | private ZstdInputStreamNoFinalizer zstdIs; | 47 | 50 |
| @@ -62,6 +65,7 @@ public ZstdDecoder() { | 62 | 65 |
| 63 | 66 | public ZstdDecoder(int maximumAllocationSize) { | 64 | 67 | this.maximumAllocationSize = ObjectUtil.checkPositiveOrZero(maximumAllocationSize, "maximumAllocationSize"); | | 68 | + this.maxForwardBytes = maximumAllocationSize > 0 ? maximumAllocationSize : DEFAULT_MAX_FORWARD_BYTES; | 65 | 69 | } | 66 | 70 |
| 67 | 71 | @Override | @@ -101,13 +105,18 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t | 101 | 105 | } | 102 | 106 | do { | 103 | 107 | w = outBuffer.writeBytes(zstdIs, outBuffer.writableBytes()); | 104 | | - } while (w != -1 && outBuffer.isWritable()); | 105 | | - if (outBuffer.isReadable()) { | | 108 | + } while (w > 0 && outBuffer.isWritable()); | | 109 | + if (!outBuffer.isWritable() || outBuffer.readableBytes() >= maxForwardBytes) { | 106 | 110 | needsRead = false; | 107 | 111 | ctx.fireChannelRead(outBuffer); | 108 | 112 | outBuffer = null; | 109 | 113 | } | 110 | | - } while (w != -1); | | 114 | + } while (w > 0); | | 115 | + if (outBuffer != null && outBuffer.isReadable()) { | | 116 | + needsRead = false; | | 117 | + ctx.fireChannelRead(outBuffer); | | 118 | + outBuffer = null; | | 119 | + } | 111 | 120 | } finally { | 112 | 121 | if (outBuffer != null) { | 113 | 122 | outBuffer.release(); | |
|