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: respect auto-read when toggled while dequeueing · schiemon/netty@5a521c8

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
+58-51Lines changed: 58 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ public class FlowControlHandler extends ChannelDuplexHandler {
9595
*/
9696
private int unsatisfiedReads;
9797

98+
/**
99+
* {@code true} while a {@link #dequeue(ChannelHandlerContext)} loop is on the stack. A
100+
* {@code fireChannelRead} may re-enter {@code read()}/{@code channelRead()}; only the outermost call
101+
* may complete the read cycle, so nested calls must not fire a second {@code channelReadComplete}.
102+
*/
103+
private boolean dequeuing;
104+
98105
public FlowControlHandler() {
99106
this(true);
100107
}
@@ -142,7 +149,11 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
142149
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
143150
super.handlerRemoved(ctx);
144151
if (!isQueueEmpty()) {
145-
dequeueAll(ctx);
152+
// The handler is being removed, so it can no longer hold messages back: flush the whole queue
153+
// downstream regardless of the auto-read state. Marking every queued message as an unsatisfied
154+
// read lets dequeue() drain all of them.
155+
unsatisfiedReads = queue.size();
156+
dequeue(ctx);
146157
ctx.fireChannelReadComplete();
147158
}
148159
destroy();
@@ -156,22 +167,23 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
156167

157168
@Override
158169
public void read(ChannelHandlerContext ctx) throws Exception {
159-
if (config.isAutoRead()) {
160-
dequeueAll(ctx);
161-
ctx.read();
162-
} else {
170+
if (!config.isAutoRead()) {
163171
unsatisfiedReads++;
172+
}
164173

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-
}
174+
if (!dequeue(ctx) || config.isAutoRead()) {
175+
assert config.isAutoRead() || unsatisfiedReads > 0;
176+
// We either could not satisfy the read or auto-read is on.
177+
// In both cases we need to delegate the read upstream.
178+
ctx.read();
179+
} else if (unsatisfiedReads == 0 && !dequeuing) {
180+
// Auto-read is off, and we have satisfied all reads.
181+
// As such, we can complete the current read cycle. A nested read (dequeuing == true) leaves
182+
// completion to the outermost call so the cycle is completed exactly once.
183+
ctx.fireChannelReadComplete();
184+
} else {
185+
// Auto-read is off and either reads are still unsatisfied or we are nested in a dequeue.
186+
// Wait for the outermost call, an upstream channelRead() or a channelReadComplete().
175187
}
176188
}
177189

@@ -183,12 +195,8 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
183195

184196
queue.offer(msg);
185197

186-
if (config.isAutoRead()) {
187-
dequeueAll(ctx);
188-
} else if (unsatisfiedReads > 0) {
189-
dequeueOne(ctx);
190-
191-
if (unsatisfiedReads == 0) {
198+
if (dequeue(ctx)) {
199+
if (!config.isAutoRead() && unsatisfiedReads == 0 && !dequeuing) {
192200
ctx.fireChannelReadComplete();
193201
}
194202
}
@@ -204,45 +212,44 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
204212
}
205213
}
206214

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-
215215
/**
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.
216+
* Dequeues messages while auto-read is enabled or downstream reads are unsatisfied, and updates
217+
* {@code unsatisfiedReads} accordingly.
219218
*
220219
* @see #read(ChannelHandlerContext)
221220
* @see #channelRead(ChannelHandlerContext, Object)
222221
*/
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-
}
222+
private boolean dequeue(ChannelHandlerContext ctx) {
223+
boolean didSatisfyARead = false;
224+
225+
boolean wasDequeuing = dequeuing;
226+
dequeuing = true;
227+
try {
228+
// fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that
229+
// we need to check if queue was set to null in the meantime and, if so, break out of the loop.
230+
while (queue != null && (config.isAutoRead() || unsatisfiedReads > 0)) {
231+
Object msg = queue.poll();
232+
if (msg == null) {
233+
break;
234+
}
233235

234-
++consumed;
235-
ctx.fireChannelRead(msg);
236-
}
236+
if (unsatisfiedReads > 0) {
237+
unsatisfiedReads--;
238+
}
239+
ctx.fireChannelRead(msg);
237240

238-
if (queue != null && queue.isEmpty()) {
239-
queue.recycle();
240-
queue = null;
241-
}
241+
didSatisfyARead = true;
242+
}
242243

243-
unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0);
244+
if (queue != null && queue.isEmpty()) {
245+
queue.recycle();
246+
queue = null;
247+
}
244248

245-
return consumed;
249+
return didSatisfyARead;
250+
} finally {
251+
dequeuing = wasDequeuing;
252+
}
246253
}
247254

248255
/**

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.