@@ -75,19 +75,21 @@ public class FlowControlHandler extends ChannelDuplexHandler { |
75 | 75 |
|
76 | 76 | /** |
77 | 77 | * Number of unsatisfied downstream {@code read()} calls. A downstream {@code read()} is considered unsatisfied |
78 | | - * if it has not yet been paired with a {@code fireChannelRead} or a cumulative {@code fireChannelReadComplete}. |
| 78 | + * if auto-read is off and if it has not yet been paired with a {@code fireChannelRead} or |
| 79 | + * a cumulative {@code fireChannelReadComplete}. |
79 | 80 | * <p> |
80 | 81 | * A {@code read()} can be satisfied in three ways, whichever comes first: |
81 | 82 | * <ul> |
82 | 83 | * <li>inside the {@code read()} call itself, by {@code dequeue()}ing a message</li> |
83 | 84 | * <li>in a {@code channelRead()}</li> |
84 | 85 | * <li>in a {@code channelReadComplete()}</li> |
85 | 86 | * </ul> |
| 87 | + * A {@code read()} can be satisfied with auto-read on. |
86 | 88 | * <p> |
87 | 89 | * When one or more {@code read()} calls are unsatisfied, a downstream {@code channelReadComplete} is fired |
88 | 90 | * only when either of the following happens: |
89 | 91 | * <ul> |
90 | | - * <li>{@code unsatisfiedReads} returns to zero after {@code dequeue()}ing, or</li> |
| 92 | + * <li>auto-read is off and {@code unsatisfiedReads} returns to zero after {@code dequeue()}ing, or</li> |
91 | 93 | * <li>an upstream {@code channelReadComplete} arrives</li> |
92 | 94 | * </ul> |
93 | 95 | */ |
@@ -140,7 +142,7 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception { |
140 | 142 | public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
141 | 143 | super.handlerRemoved(ctx); |
142 | 144 | if (!isQueueEmpty()) { |
143 | | - dequeue(ctx, queue.size()); |
| 145 | + dequeueAll(ctx); |
144 | 146 | ctx.fireChannelReadComplete(); |
145 | 147 | } |
146 | 148 | destroy(); |
@@ -154,12 +156,22 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
154 | 156 |
|
155 | 157 | @Override |
156 | 158 | public void read(ChannelHandlerContext ctx) throws Exception { |
157 | | - unsatisfiedReads++; |
158 | | - |
159 | | - if (dequeue(ctx, 1) > 0) { |
160 | | - satisfyRead(ctx); |
161 | | - } else { |
| 159 | + if (config.isAutoRead()) { |
| 160 | + dequeueAll(ctx); |
162 | 161 | ctx.read(); |
| 162 | + } else { |
| 163 | + unsatisfiedReads++; |
| 164 | + |
| 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 | + } |
163 | 175 | } |
164 | 176 | } |
165 | 177 |
|
@@ -171,56 +183,49 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception |
171 | 183 |
|
172 | 184 | queue.offer(msg); |
173 | 185 |
|
174 | | - // Snapshot before dequeue(), which may re-enter read() via fireChannelRead(...). |
175 | | - boolean shouldSatisfyRead = unsatisfiedReads > 0; |
176 | | - // We just received one message. Do we need to relay it regardless |
177 | | - // of the auto-reading configuration? The answer is yes if this |
178 | | - // method call originates from a prior read call. |
179 | | - int minConsume = shouldSatisfyRead? 1 : 0; |
| 186 | + if (config.isAutoRead()) { |
| 187 | + dequeueAll(ctx); |
| 188 | + } else if (unsatisfiedReads > 0) { |
| 189 | + dequeueOne(ctx); |
180 | 190 |
|
181 | | - if (dequeue(ctx, minConsume) > 0 && shouldSatisfyRead) { |
182 | | - satisfyRead(ctx); |
| 191 | + if (unsatisfiedReads == 0) { |
| 192 | + ctx.fireChannelReadComplete(); |
| 193 | + } |
183 | 194 | } |
184 | 195 | } |
185 | 196 |
|
186 | 197 | @Override |
187 | 198 | public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
188 | 199 | // Upstream closed the read cycle. Collapse every outstanding read() into a single downstream |
189 | 200 | // channelReadComplete; spurious upstream completions with no pending read are dropped. |
190 | | - satisfyAllReads(ctx); |
191 | | - } |
192 | | - |
193 | | - private void satisfyRead(ChannelHandlerContext ctx) { |
194 | | - if (unsatisfiedReads > 0 && --unsatisfiedReads == 0) { |
| 201 | + if (config.isAutoRead() || unsatisfiedReads > 0) { |
| 202 | + unsatisfiedReads = 0; |
195 | 203 | ctx.fireChannelReadComplete(); |
196 | 204 | } |
197 | 205 | } |
198 | 206 |
|
199 | | - private void satisfyAllReads(ChannelHandlerContext ctx) { |
200 | | - if (unsatisfiedReads > 0) { |
201 | | - unsatisfiedReads = 0; |
202 | | - ctx.fireChannelReadComplete(); |
203 | | - } |
| 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); |
204 | 213 | } |
205 | 214 |
|
206 | 215 | /** |
207 | | - * Dequeues one or many (or none) messages depending on the channel's auto |
208 | | - * reading state and returns the number of messages that were consumed from |
209 | | - * the internal queue. |
210 | | - * <p> |
211 | | - * The {@code minConsume} argument is used to force {@code dequeue()} into |
212 | | - * consuming that number of messages regardless of the channel's auto |
213 | | - * reading configuration. |
| 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. |
214 | 219 | * |
215 | 220 | * @see #read(ChannelHandlerContext) |
216 | 221 | * @see #channelRead(ChannelHandlerContext, Object) |
217 | 222 | */ |
218 | | - private int dequeue(ChannelHandlerContext ctx, int minConsume) { |
| 223 | + private int dequeue(ChannelHandlerContext ctx, int maxConsume) { |
219 | 224 | int consumed = 0; |
220 | 225 |
|
221 | 226 | // fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that |
222 | 227 | // we need to check if queue was set to null in the meantime and, if so, break out of the loop. |
223 | | - while (queue != null && (consumed < minConsume || config.isAutoRead())) { |
| 228 | + while (queue != null && (consumed < maxConsume || maxConsume < 0)) { |
224 | 229 | Object msg = queue.poll(); |
225 | 230 | if (msg == null) { |
226 | 231 | break; |
@@ -235,6 +240,8 @@ private int dequeue(ChannelHandlerContext ctx, int minConsume) { |
235 | 240 | queue = null; |
236 | 241 | } |
237 | 242 |
|
| 243 | + unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0); |
| 244 | + |
238 | 245 | return consumed; |
239 | 246 | } |
240 | 247 |
|
|