@@ -95,6 +95,13 @@ public class FlowControlHandler extends ChannelDuplexHandler { |
95 | 95 | */ |
96 | 96 | private int unsatisfiedReads; |
97 | 97 |
|
| 98 | + /** |
| 99 | + * {@code true} while a {@link #dequeue(ChannelHandlerContext)} loop is on the stack. A |
| 100 | + * {@code fireChannelRead} may re-enter {@code read()}/{@code channelRead()}; only the outermost call |
| 101 | + * may complete the read cycle, so nested calls must not fire a second {@code channelReadComplete}. |
| 102 | + */ |
| 103 | + private boolean dequeuing; |
| 104 | + |
98 | 105 | public FlowControlHandler() { |
99 | 106 | this(true); |
100 | 107 | } |
@@ -142,7 +149,11 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
142 | 149 | public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
143 | 150 | super.handlerRemoved(ctx); |
144 | 151 | if (!isQueueEmpty()) { |
145 | | - dequeueAll(ctx); |
| 152 | + // The handler is being removed, so it can no longer hold messages back: flush the whole queue |
| 153 | + // downstream regardless of the auto-read state. Marking every queued message as an unsatisfied |
| 154 | + // read lets dequeue() drain all of them. |
| 155 | + unsatisfiedReads = queue.size(); |
| 156 | + dequeue(ctx); |
146 | 157 | ctx.fireChannelReadComplete(); |
147 | 158 | } |
148 | 159 | destroy(); |
@@ -156,22 +167,23 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
156 | 167 |
|
157 | 168 | @Override |
158 | 169 | public void read(ChannelHandlerContext ctx) throws Exception { |
159 | | - if (config.isAutoRead()) { |
160 | | - dequeueAll(ctx); |
161 | | - ctx.read(); |
162 | | - } else { |
| 170 | + if (!config.isAutoRead()) { |
163 | 171 | unsatisfiedReads++; |
| 172 | + } |
164 | 173 |
|
165 | | - if (dequeueOne(ctx)) { |
166 | | - if (unsatisfiedReads == 0) { |
167 | | - ctx.fireChannelReadComplete(); |
168 | | - } |
169 | | - } else { |
170 | | - // Could not satisfy the read() from the queue. |
171 | | - // We need to request data from upstream so we can satisfy the read() in channelRead() or |
172 | | - // channelReadComplete() if it is going to be an empty read. |
173 | | - ctx.read(); |
174 | | - } |
| 174 | + if (!dequeue(ctx) || config.isAutoRead()) { |
| 175 | + assert config.isAutoRead() || unsatisfiedReads > 0; |
| 176 | + // We either could not satisfy the read or auto-read is on. |
| 177 | + // In both cases we need to delegate the read upstream. |
| 178 | + ctx.read(); |
| 179 | + } else if (unsatisfiedReads == 0 && !dequeuing) { |
| 180 | + // Auto-read is off, and we have satisfied all reads. |
| 181 | + // As such, we can complete the current read cycle. A nested read (dequeuing == true) leaves |
| 182 | + // completion to the outermost call so the cycle is completed exactly once. |
| 183 | + ctx.fireChannelReadComplete(); |
| 184 | + } else { |
| 185 | + // Auto-read is off and either reads are still unsatisfied or we are nested in a dequeue. |
| 186 | + // Wait for the outermost call, an upstream channelRead() or a channelReadComplete(). |
175 | 187 | } |
176 | 188 | } |
177 | 189 |
|
@@ -183,12 +195,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception |
183 | 195 |
|
184 | 196 | queue.offer(msg); |
185 | 197 |
|
186 | | - if (config.isAutoRead()) { |
187 | | - dequeueAll(ctx); |
188 | | - } else if (unsatisfiedReads > 0) { |
189 | | - dequeueOne(ctx); |
190 | | - |
191 | | - if (unsatisfiedReads == 0) { |
| 198 | + if (dequeue(ctx)) { |
| 199 | + if (!config.isAutoRead() && unsatisfiedReads == 0 && !dequeuing) { |
192 | 200 | ctx.fireChannelReadComplete(); |
193 | 201 | } |
194 | 202 | } |
@@ -204,45 +212,44 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
204 | 212 | } |
205 | 213 | } |
206 | 214 |
|
207 | | - private boolean dequeueOne(ChannelHandlerContext ctx) { |
208 | | - return dequeue(ctx, 1) > 0; |
209 | | - } |
210 | | - |
211 | | - private int dequeueAll(ChannelHandlerContext ctx) { |
212 | | - return dequeue(ctx, -1); |
213 | | - } |
214 | | - |
215 | 215 | /** |
216 | | - * Dequeues up to {@code maxConsume} messages, fires them downstream and |
217 | | - * updates {@code unsatisfiedReads} accordingly. If {@code maxConsume} is negative, |
218 | | - * there is no upper limit on the number of messages to dequeue and fire downstream. |
| 216 | + * Dequeues messages while auto-read is enabled or downstream reads are unsatisfied, and updates |
| 217 | + * {@code unsatisfiedReads} accordingly. |
219 | 218 | * |
220 | 219 | * @see #read(ChannelHandlerContext) |
221 | 220 | * @see #channelRead(ChannelHandlerContext, Object) |
222 | 221 | */ |
223 | | - private int dequeue(ChannelHandlerContext ctx, int maxConsume) { |
224 | | - int consumed = 0; |
225 | | - |
226 | | - // fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that |
227 | | - // we need to check if queue was set to null in the meantime and, if so, break out of the loop. |
228 | | - while (queue != null && (consumed < maxConsume || maxConsume < 0)) { |
229 | | - Object msg = queue.poll(); |
230 | | - if (msg == null) { |
231 | | - break; |
232 | | - } |
| 222 | + private boolean dequeue(ChannelHandlerContext ctx) { |
| 223 | + boolean didSatisfyARead = false; |
| 224 | + |
| 225 | + boolean wasDequeuing = dequeuing; |
| 226 | + dequeuing = true; |
| 227 | + try { |
| 228 | + // fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that |
| 229 | + // we need to check if queue was set to null in the meantime and, if so, break out of the loop. |
| 230 | + while (queue != null && (config.isAutoRead() || unsatisfiedReads > 0)) { |
| 231 | + Object msg = queue.poll(); |
| 232 | + if (msg == null) { |
| 233 | + break; |
| 234 | + } |
233 | 235 |
|
234 | | - ++consumed; |
235 | | - ctx.fireChannelRead(msg); |
236 | | - } |
| 236 | + if (unsatisfiedReads > 0) { |
| 237 | + unsatisfiedReads--; |
| 238 | + } |
| 239 | + ctx.fireChannelRead(msg); |
237 | 240 |
|
238 | | - if (queue != null && queue.isEmpty()) { |
239 | | - queue.recycle(); |
240 | | - queue = null; |
241 | | - } |
| 241 | + didSatisfyARead = true; |
| 242 | + } |
242 | 243 |
|
243 | | - unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0); |
| 244 | + if (queue != null && queue.isEmpty()) { |
| 245 | + queue.recycle(); |
| 246 | + queue = null; |
| 247 | + } |
244 | 248 |
|
245 | | - return consumed; |
| 249 | + return didSatisfyARead; |
| 250 | + } finally { |
| 251 | + dequeuing = wasDequeuing; |
| 252 | + } |
246 | 253 | } |
247 | 254 |
|
248 | 255 | /** |
|