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@4a2aead

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
+53-51Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ 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.
100+
*/
101+
private boolean dequeuing;
102+
98103
public FlowControlHandler() {
99104
this(true);
100105
}
@@ -142,7 +147,8 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
142147
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
143148
super.handlerRemoved(ctx);
144149
if (!isQueueEmpty()) {
145-
dequeueAll(ctx);
150+
unsatisfiedReads = queue.size();
151+
dequeue(ctx);
146152
ctx.fireChannelReadComplete();
147153
}
148154
destroy();
@@ -156,22 +162,23 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
156162

157163
@Override
158164
public void read(ChannelHandlerContext ctx) throws Exception {
159-
if (config.isAutoRead()) {
160-
dequeueAll(ctx);
161-
ctx.read();
162-
} else {
165+
if (!config.isAutoRead()) {
163166
unsatisfiedReads++;
167+
}
164168

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-
}
169+
if (!dequeue(ctx) || config.isAutoRead()) {
170+
assert unsatisfiedReads > 0 || config.isAutoRead();
171+
// We either could not satisfy the read or auto-read is on.
172+
// In both cases we need to delegate the read upstream.
173+
ctx.read();
174+
} else if (unsatisfiedReads == 0 && !dequeuing) {
175+
// Auto-read is off, and we have satisfied all reads.
176+
// As such, we can complete the current read cycle. && !dequeueing makes sure we are completing the
177+
// read cycle only once in the top-most read() call.
178+
ctx.fireChannelReadComplete();
179+
} else {
180+
// Auto-read is off, and either reads are still unsatisfied or we are nested in a dequeue.
181+
// Wait for the outermost call, an upstream channelRead() or a channelReadComplete().
175182
}
176183
}
177184

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

184191
queue.offer(msg);
185192

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 && !dequeuing) {
192195
ctx.fireChannelReadComplete();
193196
}
194197
}
@@ -204,45 +207,44 @@ public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
204207
}
205208
}
206209

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-
215210
/**
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.
219213
*
220214
* @see #read(ChannelHandlerContext)
221215
* @see #channelRead(ChannelHandlerContext, Object)
222216
*/
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-
}
217+
private boolean dequeue(ChannelHandlerContext ctx) {
218+
boolean didSatisfyARead = false;
219+
220+
boolean wasDequeuing = dequeuing;
221+
dequeuing = true;
222+
try {
223+
// fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that
224+
// we need to check if queue was set to null in the meantime and, if so, break out of the loop.
225+
while (queue != null && (config.isAutoRead() || unsatisfiedReads > 0)) {
226+
Object msg = queue.poll();
227+
if (msg == null) {
228+
break;
229+
}
233230

234-
++consumed;
235-
ctx.fireChannelRead(msg);
236-
}
231+
if (unsatisfiedReads > 0) {
232+
unsatisfiedReads--;
233+
}
234+
ctx.fireChannelRead(msg);
237235

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

243-
unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0);
239+
if (queue != null && queue.isEmpty()) {
240+
queue.recycle();
241+
queue = null;
242+
}
244243

245-
return consumed;
244+
return didSatisfyARead;
245+
} finally {
246+
dequeuing = wasDequeuing;
247+
}
246248
}
247249

248250
/**

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.