|
28 | 28 | * downstream backpressure. |
29 | 29 | */ |
30 | 30 | public final class BackpressureDecompressionHandler extends ChannelDuplexHandler { |
31 | | - private final Decompressor decompressor; |
| 31 | + private static final int DEFAULT_MAX_MESSAGES_PER_READ = 32; |
| 32 | + |
| 33 | + private final Decompressor.AbstractDecompressorBuilder decompressorBuilder; |
| 34 | + /** |
| 35 | + * Decompressor is created lazily to avoid memory leaks when a BackpressureDecompressionHandler is never added to a |
| 36 | + * channel. |
| 37 | + */ |
| 38 | + private Decompressor decompressor; |
| 39 | + private final int maxMessagesPerRead; |
32 | 40 | private RecyclableArrayList inputBuffer; |
33 | 41 | private boolean closed; |
34 | | - private boolean downstreamWantsMore; |
| 42 | + private int downstreamWantsMore; |
35 | 43 | private boolean reading; |
36 | 44 |
|
| 45 | + BackpressureDecompressionHandler(Builder builder) { |
| 46 | + this.decompressorBuilder = builder.decompressorBuilder; |
| 47 | + this.maxMessagesPerRead = builder.maxMessagesPerRead; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create a new handler builder. |
| 52 | + * |
| 53 | + * @param decompressorBuilder The decompressor builder to use. This builder is invoked lazily when the handler |
| 54 | + * is added to the pipeline. |
| 55 | + * @return The builder for the decompression handler |
| 56 | + */ |
| 57 | + public static Builder builder(Decompressor.AbstractDecompressorBuilder decompressorBuilder) { |
| 58 | + return new Builder(decompressorBuilder); |
| 59 | + } |
| 60 | + |
37 | 61 | /** |
38 | | - * Create a new handler using the given decompressor. |
| 62 | + * Create a new decompression handler with default settings. Equivalent to |
| 63 | + * {@code builder(decompressorBuilder).build()}. |
39 | 64 | * |
40 | | - * @param decompressor The decompressor |
| 65 | + * @param decompressorBuilder The decompressor builder to use. This builder is invoked lazily when the handler |
| 66 | + * is added to the pipeline. |
| 67 | + * @return The decompression handler |
41 | 68 | */ |
42 | | - public BackpressureDecompressionHandler(Decompressor decompressor) { |
43 | | - this.decompressor = ObjectUtil.checkNotNull(decompressor, "decompressor"); |
| 69 | + public static BackpressureDecompressionHandler create( |
| 70 | + Decompressor.AbstractDecompressorBuilder decompressorBuilder) { |
| 71 | + return builder(decompressorBuilder).build(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
| 76 | + decompressor = decompressorBuilder.build(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
| 81 | + if (!closed) { |
| 82 | + decompressor.close(); |
| 83 | + closed = true; |
| 84 | + } |
44 | 85 | } |
45 | 86 |
|
46 | 87 | private void handleException(Exception e) { |
@@ -62,7 +103,11 @@ private Decompressor.Status status() { |
62 | 103 | } |
63 | 104 |
|
64 | 105 | private boolean downstreamWantsMore(ChannelHandlerContext ctx) { |
65 | | - return downstreamWantsMore || ctx.channel().config().isAutoRead(); |
| 106 | + return downstreamWantsMore > 0 || isAutoRead(ctx); |
| 107 | + } |
| 108 | + |
| 109 | + private static boolean isAutoRead(ChannelHandlerContext ctx) { |
| 110 | + return ctx.channel().config().isAutoRead(); |
66 | 111 | } |
67 | 112 |
|
68 | 113 | private void processSome(ChannelHandlerContext ctx) { |
@@ -82,9 +127,14 @@ private void processSome(ChannelHandlerContext ctx) { |
82 | 127 | buf.release(); |
83 | 128 | break; |
84 | 129 | } |
85 | | - downstreamWantsMore = false; |
| 130 | + int newDemand = --downstreamWantsMore; |
| 131 | + if (newDemand == 0 && isAutoRead(ctx)) { |
| 132 | + downstreamWantsMore = maxMessagesPerRead; |
| 133 | + } |
86 | 134 | ctx.fireChannelRead(buf); |
87 | | - ctx.fireChannelReadComplete(); |
| 135 | + if (newDemand == 0) { |
| 136 | + ctx.fireChannelReadComplete(); |
| 137 | + } |
88 | 138 | break; |
89 | 139 | case NEED_INPUT: |
90 | 140 | if (inputBuffer == null) { |
@@ -138,20 +188,69 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception |
138 | 188 | @Override |
139 | 189 | public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
140 | 190 | reading = false; |
141 | | - if (!ctx.channel().config().isAutoRead() && downstreamWantsMore) { |
142 | | - ctx.read(); |
143 | | - } |
| 191 | + endOfRead(ctx, false); |
144 | 192 | } |
145 | 193 |
|
146 | 194 | @Override |
147 | 195 | public void read(ChannelHandlerContext ctx) throws Exception { |
148 | 196 | if (closed) { |
149 | 197 | return; |
150 | 198 | } |
151 | | - downstreamWantsMore = true; |
| 199 | + downstreamWantsMore = maxMessagesPerRead; |
152 | 200 | processSome(ctx); |
153 | | - if (downstreamWantsMore && !reading) { |
154 | | - ctx.read(); |
| 201 | + if (!reading) { |
| 202 | + endOfRead(ctx, true); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Called when we just finished processing a batch of data and no more upstream data is forthcoming until we ask |
| 208 | + * for it. This method checks for downstream demand and potentially sends an upstream read request. |
| 209 | + * |
| 210 | + * @param ctx The context |
| 211 | + * @param forceRead Whether to force an upstream .read even if auto read is on |
| 212 | + */ |
| 213 | + private void endOfRead(ChannelHandlerContext ctx, boolean forceRead) { |
| 214 | + if (downstreamWantsMore == maxMessagesPerRead) { |
| 215 | + // no new data, ask upstream |
| 216 | + if (forceRead || !isAutoRead(ctx)) { |
| 217 | + ctx.read(); |
| 218 | + } |
| 219 | + } else if (downstreamWantsMore > 0) { |
| 220 | + // we forwarded some data, let's wait for downstream demand before asking for more |
| 221 | + downstreamWantsMore = isAutoRead(ctx) ? maxMessagesPerRead : 0; |
| 222 | + ctx.fireChannelReadComplete(); |
| 223 | + } // if downstreamWantsMore == 0, we already fired readComplete |
| 224 | + } |
| 225 | + |
| 226 | + public static final class Builder { |
| 227 | + private final Decompressor.AbstractDecompressorBuilder decompressorBuilder; |
| 228 | + private int maxMessagesPerRead = DEFAULT_MAX_MESSAGES_PER_READ; |
| 229 | + |
| 230 | + Builder(Decompressor.AbstractDecompressorBuilder decompressorBuilder) { |
| 231 | + this.decompressorBuilder = ObjectUtil.checkNotNull(decompressorBuilder, "decompressorBuilder"); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * The maximum number of buffers to send down the pipeline before a new downstream read request is required. If |
| 236 | + * the channel has auto read enabled, more buffers than this may be sent. |
| 237 | + * |
| 238 | + * @param maxMessagesPerRead The maximum number of buffers to send per single read request |
| 239 | + * @return This builder |
| 240 | + */ |
| 241 | + public Builder maxMessagesPerRead(int maxMessagesPerRead) { |
| 242 | + this.maxMessagesPerRead = ObjectUtil.checkPositive(maxMessagesPerRead, "maxMessagesPerRead"); |
| 243 | + return this; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Create a new decompression handler. Note that the actual decompressor is only created when the handler is |
| 248 | + * added to a pipeline, to avoid memory leaks if the handler is never used. |
| 249 | + * |
| 250 | + * @return The handler |
| 251 | + */ |
| 252 | + public BackpressureDecompressionHandler build() { |
| 253 | + return new BackpressureDecompressionHandler(this); |
155 | 254 | } |
156 | 255 | } |
157 | 256 | } |