|
32 | 32 |
|
33 | 33 | /** |
34 | 34 | * The {@link FlowControlHandler} ensures that only one message per {@code read()} is sent downstream. |
35 | | - * |
| 35 | + * <p> |
36 | 36 | * Classes such as {@link ByteToMessageDecoder} or {@link MessageToByteEncoder} are free to emit as |
37 | 37 | * many events as they like for any given input. A channel's auto reading configuration doesn't usually |
38 | 38 | * apply in these scenarios. This is causing problems in downstream {@link ChannelHandler}s that would |
@@ -73,7 +73,25 @@ public class FlowControlHandler extends ChannelDuplexHandler { |
73 | 73 |
|
74 | 74 | private ChannelConfig config; |
75 | 75 |
|
76 | | - private boolean shouldConsume; |
| 76 | + /** |
| 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}. |
| 79 | + * <p> |
| 80 | + * A {@code read()} can be satisfied in three ways, whichever comes first: |
| 81 | + * <ul> |
| 82 | + * <li>inside the {@code read()} call itself, by {@code dequeue()}ing a message</li> |
| 83 | + * <li>in a {@code channelRead()}</li> |
| 84 | + * <li>in a {@code channelReadComplete()}</li> |
| 85 | + * </ul> |
| 86 | + * <p> |
| 87 | + * When one or more {@code read()} calls are unsatisfied, a downstream {@code channelReadComplete} is fired |
| 88 | + * only when either of the following happens: |
| 89 | + * <ul> |
| 90 | + * <li>{@code unsatisfiedReads} returns to zero after {@code dequeue()}ing, or</li> |
| 91 | + * <li>an upstream {@code channelReadComplete} arrives</li> |
| 92 | + * </ul> |
| 93 | + */ |
| 94 | + private int unsatisfiedReads; |
77 | 95 |
|
78 | 96 | public FlowControlHandler() { |
79 | 97 | this(true); |
@@ -123,6 +141,7 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception { |
123 | 141 | super.handlerRemoved(ctx); |
124 | 142 | if (!isQueueEmpty()) { |
125 | 143 | dequeue(ctx, queue.size()); |
| 144 | + ctx.fireChannelReadComplete(); |
126 | 145 | } |
127 | 146 | destroy(); |
128 | 147 | } |
@@ -135,13 +154,11 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception { |
135 | 154 |
|
136 | 155 | @Override |
137 | 156 | public void read(ChannelHandlerContext ctx) throws Exception { |
138 | | - if (dequeue(ctx, 1) == 0) { |
139 | | - // It seems no messages were consumed. We need to read() some |
140 | | - // messages from upstream and once one arrives it need to be |
141 | | - // relayed to downstream to keep the flow going. |
142 | | - shouldConsume = true; |
143 | | - ctx.read(); |
144 | | - } else if (config.isAutoRead()) { |
| 157 | + unsatisfiedReads++; |
| 158 | + |
| 159 | + if (dequeue(ctx, 1) > 0) { |
| 160 | + satisfyRead(ctx); |
| 161 | + } else { |
145 | 162 | ctx.read(); |
146 | 163 | } |
147 | 164 | } |
@@ -154,31 +171,43 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception |
154 | 171 |
|
155 | 172 | queue.offer(msg); |
156 | 173 |
|
| 174 | + // Snapshot before dequeue(), which may re-enter read() via fireChannelRead(...). |
| 175 | + boolean shouldSatisfyRead = unsatisfiedReads > 0; |
157 | 176 | // We just received one message. Do we need to relay it regardless |
158 | | - // of the auto reading configuration? The answer is yes if this |
159 | | - // method was called as a result of a prior read() call. |
160 | | - int minConsume = shouldConsume ? 1 : 0; |
161 | | - shouldConsume = false; |
| 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; |
162 | 180 |
|
163 | | - dequeue(ctx, minConsume); |
| 181 | + if (dequeue(ctx, minConsume) > 0 && shouldSatisfyRead) { |
| 182 | + satisfyRead(ctx); |
| 183 | + } |
164 | 184 | } |
165 | 185 |
|
166 | 186 | @Override |
167 | 187 | public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
168 | | - if (isQueueEmpty()) { |
| 188 | + // Upstream closed the read cycle. Collapse every outstanding read() into a single downstream |
| 189 | + // 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) { |
| 195 | + ctx.fireChannelReadComplete(); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private void satisfyAllReads(ChannelHandlerContext ctx) { |
| 200 | + if (unsatisfiedReads > 0) { |
| 201 | + unsatisfiedReads = 0; |
169 | 202 | ctx.fireChannelReadComplete(); |
170 | | - } else { |
171 | | - // Don't relay completion events from upstream as they |
172 | | - // make no sense in this context. See dequeue() where |
173 | | - // a new set of completion events is being produced. |
174 | 203 | } |
175 | 204 | } |
176 | 205 |
|
177 | 206 | /** |
178 | 207 | * Dequeues one or many (or none) messages depending on the channel's auto |
179 | 208 | * reading state and returns the number of messages that were consumed from |
180 | 209 | * the internal queue. |
181 | | - * |
| 210 | + * <p> |
182 | 211 | * The {@code minConsume} argument is used to force {@code dequeue()} into |
183 | 212 | * consuming that number of messages regardless of the channel's auto |
184 | 213 | * reading configuration. |
@@ -189,8 +218,8 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
189 | 218 | private int dequeue(ChannelHandlerContext ctx, int minConsume) { |
190 | 219 | int consumed = 0; |
191 | 220 |
|
192 | | - // fireChannelRead(...) may call ctx.read() and so this method may reentrance. Because of this we need to |
193 | | - // check if queue was set to null in the meantime and if so break the loop. |
| 221 | + // fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that |
| 222 | + // we need to check if queue was set to null in the meantime and, if so, break out of the loop. |
194 | 223 | while (queue != null && (consumed < minConsume || config.isAutoRead())) { |
195 | 224 | Object msg = queue.poll(); |
196 | 225 | if (msg == null) { |
@@ -201,16 +230,9 @@ private int dequeue(ChannelHandlerContext ctx, int minConsume) { |
201 | 230 | ctx.fireChannelRead(msg); |
202 | 231 | } |
203 | 232 |
|
204 | | - // We're firing a completion event every time one (or more) |
205 | | - // messages were consumed and the queue ended up being drained |
206 | | - // to an empty state. |
207 | 233 | if (queue != null && queue.isEmpty()) { |
208 | 234 | queue.recycle(); |
209 | 235 | queue = null; |
210 | | - |
211 | | - if (consumed > 0) { |
212 | | - ctx.fireChannelReadComplete(); |
213 | | - } |
214 | 236 | } |
215 | 237 |
|
216 | 238 | return consumed; |
|