GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — FlowControlHandler: Fix autoRead behavior · netty/netty@601ff20

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • handler/src
    • main/java/io/netty/handler/flow
      • FlowControlHandler.java
    • test/java/io/netty/handler/flow
      • FlowControlHandlerTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java‎

Copy file name to clipboardExpand all lines: handler/src/main/java/io/netty/handler/flow/FlowControlHandler.java
+42-35Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,21 @@ public class FlowControlHandler extends ChannelDuplexHandler {
7575

7676
/**
7777
* 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}.
7980
* <p>
8081
* A {@code read()} can be satisfied in three ways, whichever comes first:
8182
* <ul>
8283
* <li>inside the {@code read()} call itself, by {@code dequeue()}ing a message</li>
8384
* <li>in a {@code channelRead()}</li>
8485
* <li>in a {@code channelReadComplete()}</li>
8586
* </ul>
87+
* A {@code read()} can be satisfied with auto-read on.
8688
* <p>
8789
* When one or more {@code read()} calls are unsatisfied, a downstream {@code channelReadComplete} is fired
8890
* only when either of the following happens:
8991
* <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>
9193
* <li>an upstream {@code channelReadComplete} arrives</li>
9294
* </ul>
9395
*/
@@ -140,7 +142,7 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
140142
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
141143
super.handlerRemoved(ctx);
142144
if (!isQueueEmpty()) {
143-
dequeue(ctx, queue.size());
145+
dequeueAll(ctx);
144146
ctx.fireChannelReadComplete();
145147
}
146148
destroy();
@@ -154,12 +156,22 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
154156

155157
@Override
156158
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);
162161
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+
}
163175
}
164176
}
165177

@@ -171,56 +183,49 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
171183

172184
queue.offer(msg);
173185

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);
180190

181-
if (dequeue(ctx, minConsume) > 0 && shouldSatisfyRead) {
182-
satisfyRead(ctx);
191+
if (unsatisfiedReads == 0) {
192+
ctx.fireChannelReadComplete();
193+
}
183194
}
184195
}
185196

186197
@Override
187198
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
188199
// Upstream closed the read cycle. Collapse every outstanding read() into a single downstream
189200
// 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;
195203
ctx.fireChannelReadComplete();
196204
}
197205
}
198206

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);
204213
}
205214

206215
/**
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.
214219
*
215220
* @see #read(ChannelHandlerContext)
216221
* @see #channelRead(ChannelHandlerContext, Object)
217222
*/
218-
private int dequeue(ChannelHandlerContext ctx, int minConsume) {
223+
private int dequeue(ChannelHandlerContext ctx, int maxConsume) {
219224
int consumed = 0;
220225

221226
// fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that
222227
// 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)) {
224229
Object msg = queue.poll();
225230
if (msg == null) {
226231
break;
@@ -235,6 +240,8 @@ private int dequeue(ChannelHandlerContext ctx, int minConsume) {
235240
queue = null;
236241
}
237242

243+
unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0);
244+
238245
return consumed;
239246
}
240247

Collapse file

‎handler/src/test/java/io/netty/handler/flow/FlowControlHandlerTest.java‎

Copy file name to clipboardExpand all lines: handler/src/test/java/io/netty/handler/flow/FlowControlHandlerTest.java
+117Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,123 @@ public void channelReadComplete(ChannelHandlerContext ctx) {
845845
assertFalse(channel.finishAndReleaseAll());
846846
}
847847

848+
@Test
849+
public void testCompleteReadOnUpstreamCompleteWhenAutoReadOn() throws Exception {
850+
final AtomicInteger reads = new AtomicInteger();
851+
final AtomicInteger readCompletes = new AtomicInteger();
852+
final EmbeddedChannel channel = new EmbeddedChannel(
853+
false, false,
854+
new FlowControlHandler(),
855+
new ChannelInboundHandlerAdapter() {
856+
@Override
857+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
858+
reads.incrementAndGet();
859+
}
860+
861+
@Override
862+
public void channelReadComplete(ChannelHandlerContext ctx) {
863+
readCompletes.incrementAndGet();
864+
}
865+
});
866+
867+
assertTrue(channel.config().isAutoRead());
868+
channel.register();
869+
870+
channel.writeOneInbound("msg1").syncUninterruptibly();
871+
channel.writeOneInbound("msg2").syncUninterruptibly();
872+
channel.writeOneInbound("msg3").syncUninterruptibly();
873+
874+
// All three messages must arrive before channelReadComplete signals end-of-batch.
875+
assertEquals(3, reads.get());
876+
// As auto-read is on, FlowControlHandler should not fire a channelReadComplete on its own but should wait
877+
// for upstream to fire it.
878+
assertEquals(0, readCompletes.get());
879+
880+
// Upstream now fires channelReadComplete and FlowControlHandler should pass it through.
881+
channel.flushInbound();
882+
883+
assertEquals(3, reads.get());
884+
assertEquals(1, readCompletes.get());
885+
886+
assertFalse(channel.finishAndReleaseAll());
887+
}
888+
889+
@Test
890+
public void testSatisfyPendingReadsAfterDisablingAutoRead() throws Exception {
891+
final AtomicInteger reads = new AtomicInteger();
892+
final AtomicInteger readCompletes = new AtomicInteger();
893+
final EmbeddedChannel channel = new EmbeddedChannel(
894+
false, false,
895+
new FlowControlHandler(),
896+
new ChannelInboundHandlerAdapter() {
897+
@Override
898+
public void channelRead(ChannelHandlerContext ctx, Object msg) {
899+
reads.incrementAndGet();
900+
}
901+
902+
@Override
903+
public void channelReadComplete(ChannelHandlerContext ctx) {
904+
readCompletes.incrementAndGet();
905+
}
906+
});
907+
908+
channel.config().setAutoRead(false);
909+
channel.register();
910+
911+
// We issue two reads with auto-read off. We expect at least two messages to be delivered, even when we are
912+
// going to turn off auto-read in a moment.
913+
channel.read();
914+
channel.read();
915+
channel.config().setAutoRead(true);
916+
917+
// We got a message with auto-read on. This one satisfies the first read.
918+
channel.writeOneInbound("msg1").syncUninterruptibly();
919+
920+
assertEquals(1, reads.get());
921+
assertEquals(0, readCompletes.get());
922+
923+
channel.config().setAutoRead(false);
924+
channel.config().setAutoRead(true);
925+
// In the end auto-read is off, and we have one remaining unsatisfied read.
926+
channel.config().setAutoRead(false);
927+
928+
// sanity check: nothing should happen.
929+
assertEquals(1, reads.get());
930+
assertEquals(0, readCompletes.get());
931+
932+
// This message is delivered, satisfying the second read, completing the batch.
933+
channel.writeOneInbound("msg2").syncUninterruptibly();
934+
935+
assertEquals(2, reads.get());
936+
assertEquals(1, readCompletes.get());
937+
938+
// This message is not delivered but queued.
939+
channel.writeOneInbound("msg3").syncUninterruptibly();
940+
941+
assertEquals(2, reads.get());
942+
assertEquals(1, readCompletes.get());
943+
944+
// Upstream now fires channelReadComplete and FlowControlHandler should pass it through.
945+
channel.flushInbound();
946+
947+
// We ignore it.
948+
assertEquals(2, reads.get());
949+
assertEquals(1, readCompletes.get());
950+
951+
channel.config().setAutoRead(true);
952+
953+
// The fourth message is dequeued/delivered but NOT satisfying the third read.
954+
assertEquals(3, reads.get());
955+
assertEquals(1, readCompletes.get());
956+
957+
channel.flushInbound();
958+
959+
assertEquals(3, reads.get());
960+
assertEquals(2, readCompletes.get());
961+
962+
assertFalse(channel.finishAndReleaseAll());
963+
}
964+
848965
/**
849966
* This is a fictional message decoder. It decodes each {@code byte}
850967
* into three strings.

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.