@@ -142,7 +142,12 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
142 | 142 | public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
143 | 143 | super.handlerRemoved(ctx); |
144 | 144 | if (!isQueueEmpty()) { |
145 | | - dequeueAll(ctx); |
| 145 | + // The handler is being removed, so it can no longer hold messages back: flush the whole queue |
| 146 | + // downstream regardless of the auto-read state. |
| 147 | + Object msg; |
| 148 | + while ((msg = queue.poll()) != null) { |
| 149 | + ctx.fireChannelRead(msg); |
| 150 | + } |
146 | 151 | ctx.fireChannelReadComplete(); |
147 | 152 | } |
148 | 153 | destroy(); |
@@ -156,22 +161,24 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
156 | 161 |
|
157 | 162 | @Override |
158 | 163 | public void read(ChannelHandlerContext ctx) throws Exception { |
159 | | - if (config.isAutoRead()) { |
160 | | - dequeueAll(ctx); |
161 | | - ctx.read(); |
162 | | - } else { |
| 164 | + if (!config.isAutoRead()) { |
163 | 165 | unsatisfiedReads++; |
| 166 | + } |
164 | 167 |
|
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 | | - } |
| 168 | + if (!dequeue(ctx) || config.isAutoRead()) { |
| 169 | + assert config.isAutoRead() || unsatisfiedReads > 0; |
| 170 | + // We either could not satisfy the read or auto-read is on. |
| 171 | + // In both cases we need to delegate the read upstream. |
| 172 | + ctx.read(); |
| 173 | + } else if (unsatisfiedReads == 0) { |
| 174 | + // Auto-read is off, and we have satisfied all reads. |
| 175 | + // As such, we can complete the current read cycle. |
| 176 | + ctx.fireChannelReadComplete(); |
| 177 | + } else { |
| 178 | + // Auto-read is off, and unsatisfiedReads is still > 0. dequeue() decrements unsatisfiedReads |
| 179 | + // only after fireChannelRead(), so a re-entrant (nested) read sees this read still counted here, |
| 180 | + // waits, and leaves the single channelReadComplete to the outermost call. Otherwise, wait for an |
| 181 | + // upstream channelRead() or channelReadComplete() to complete the read cycle. |
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) { |
192 | 195 | ctx.fireChannelReadComplete(); |
193 | 196 | } |
194 | 197 | } |
@@ -204,45 +207,46 @@ 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; |
| 217 | + private boolean dequeue(ChannelHandlerContext ctx) { |
| 218 | + boolean didSatisfyARead = false; |
225 | 219 |
|
226 | 220 | // fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that |
227 | 221 | // 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)) { |
| 222 | + while (queue != null && (config.isAutoRead() || unsatisfiedReads > 0)) { |
229 | 223 | Object msg = queue.poll(); |
230 | 224 | if (msg == null) { |
231 | 225 | break; |
232 | 226 | } |
233 | 227 |
|
234 | | - ++consumed; |
235 | 228 | ctx.fireChannelRead(msg); |
| 229 | + |
| 230 | + didSatisfyARead = true; |
| 231 | + if (unsatisfiedReads > 0) { |
| 232 | + unsatisfiedReads--; |
| 233 | + } |
| 234 | + |
| 235 | + if (!config.isAutoRead()) { |
| 236 | + // Auto-read is off: deliver a single message per read(). unsatisfiedReads is decremented |
| 237 | + // above, after the (possibly re-entrant) fireChannelRead, so a nested read still counts this |
| 238 | + // read in unsatisfiedReads and defers completion to the outermost call instead of |
| 239 | + // over-draining the queue. |
| 240 | + break; |
| 241 | + } |
236 | 242 | } |
237 | 243 |
|
238 | 244 | if (queue != null && queue.isEmpty()) { |
239 | 245 | queue.recycle(); |
240 | 246 | queue = null; |
241 | 247 | } |
242 | 248 |
|
243 | | - unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0); |
244 | | - |
245 | | - return consumed; |
| 249 | + return didSatisfyARead; |
246 | 250 | } |
247 | 251 |
|
248 | 252 | /** |
|