@@ -95,6 +95,11 @@ 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. |
| 100 | + */ |
| 101 | + private boolean dequeuing; |
| 102 | + |
98 | 103 | public FlowControlHandler() { |
99 | 104 | this(true); |
100 | 105 | } |
@@ -142,7 +147,8 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
142 | 147 | public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
143 | 148 | super.handlerRemoved(ctx); |
144 | 149 | if (!isQueueEmpty()) { |
145 | | - dequeueAll(ctx); |
| 150 | + unsatisfiedReads = queue.size(); |
| 151 | + dequeue(ctx); |
146 | 152 | ctx.fireChannelReadComplete(); |
147 | 153 | } |
148 | 154 | destroy(); |
@@ -156,22 +162,23 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
156 | 162 |
|
157 | 163 | @Override |
158 | 164 | public void read(ChannelHandlerContext ctx) throws Exception { |
159 | | - if (config.isAutoRead()) { |
160 | | - dequeueAll(ctx); |
161 | | - ctx.read(); |
162 | | - } else { |
| 165 | + if (!config.isAutoRead()) { |
163 | 166 | unsatisfiedReads++; |
| 167 | + } |
164 | 168 |
|
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 | | - } |
| 169 | + if (!dequeue(ctx) || config.isAutoRead()) { |
| 170 | + assert unsatisfiedReads > 0 || config.isAutoRead(); |
| 171 | + // We either could not satisfy the read or auto-read is on. |
| 172 | + // In both cases we need to delegate the read upstream. |
| 173 | + ctx.read(); |
| 174 | + } else if (unsatisfiedReads == 0 && !dequeuing) { |
| 175 | + // Auto-read is off, and we have satisfied all reads. |
| 176 | + // As such, we can complete the current read cycle. && !dequeueing makes sure we are completing the |
| 177 | + // read cycle only once in the top-most read() call. |
| 178 | + ctx.fireChannelReadComplete(); |
| 179 | + } else { |
| 180 | + // Auto-read is off, and either reads are still unsatisfied or we are nested in a dequeue. |
| 181 | + // Wait for the outermost call, an upstream channelRead() or a channelReadComplete(). |
175 | 182 | } |
176 | 183 | } |
177 | 184 |
|
@@ -183,12 +190,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception |
183 | 190 |
|
184 | 191 | queue.offer(msg); |
185 | 192 |
|
186 | | - if (config.isAutoRead()) { |
187 | | - dequeueAll(ctx); |
188 | | - } else if (unsatisfiedReads > 0) { |
189 | | - dequeueOne(ctx); |
190 | | - |
191 | | - if (unsatisfiedReads == 0) { |
| 193 | + if (dequeue(ctx)) { |
| 194 | + if (!config.isAutoRead() && unsatisfiedReads == 0 && !dequeuing) { |
192 | 195 | ctx.fireChannelReadComplete(); |
193 | 196 | } |
194 | 197 | } |
@@ -204,45 +207,44 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
204 | 207 | } |
205 | 208 | } |
206 | 209 |
|
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 | 210 | /** |
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. |
| 211 | + * Dequeues messages while auto-read is enabled or downstream reads are unsatisfied, and updates |
| 212 | + * {@code unsatisfiedReads} accordingly. |
219 | 213 | * |
220 | 214 | * @see #read(ChannelHandlerContext) |
221 | 215 | * @see #channelRead(ChannelHandlerContext, Object) |
222 | 216 | */ |
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 | | - } |
| 217 | + private boolean dequeue(ChannelHandlerContext ctx) { |
| 218 | + boolean didSatisfyARead = false; |
| 219 | + |
| 220 | + boolean wasDequeuing = dequeuing; |
| 221 | + dequeuing = true; |
| 222 | + try { |
| 223 | + // fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that |
| 224 | + // we need to check if queue was set to null in the meantime and, if so, break out of the loop. |
| 225 | + while (queue != null && (config.isAutoRead() || unsatisfiedReads > 0)) { |
| 226 | + Object msg = queue.poll(); |
| 227 | + if (msg == null) { |
| 228 | + break; |
| 229 | + } |
233 | 230 |
|
234 | | - ++consumed; |
235 | | - ctx.fireChannelRead(msg); |
236 | | - } |
| 231 | + if (unsatisfiedReads > 0) { |
| 232 | + unsatisfiedReads--; |
| 233 | + } |
| 234 | + ctx.fireChannelRead(msg); |
237 | 235 |
|
238 | | - if (queue != null && queue.isEmpty()) { |
239 | | - queue.recycle(); |
240 | | - queue = null; |
241 | | - } |
| 236 | + didSatisfyARead = true; |
| 237 | + } |
242 | 238 |
|
243 | | - unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0); |
| 239 | + if (queue != null && queue.isEmpty()) { |
| 240 | + queue.recycle(); |
| 241 | + queue = null; |
| 242 | + } |
244 | 243 |
|
245 | | - return consumed; |
| 244 | + return didSatisfyARead; |
| 245 | + } finally { |
| 246 | + dequeuing = wasDequeuing; |
| 247 | + } |
246 | 248 | } |
247 | 249 |
|
248 | 250 | /** |
|