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@725e8d4

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
+43-39Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,12 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
142142
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
143143
super.handlerRemoved(ctx);
144144
if (!isQueueEmpty()) {
145-
dequeueAll(ctx);
145+
// The handler is being removed, so it can no longer hold messages back: flush the whole queue
146+
// downstream regardless of the auto-read state.
147+
Object msg;
148+
while ((msg = queue.poll()) != null) {
149+
ctx.fireChannelRead(msg);
150+
}
146151
ctx.fireChannelReadComplete();
147152
}
148153
destroy();
@@ -156,22 +161,24 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
156161

157162
@Override
158163
public void read(ChannelHandlerContext ctx) throws Exception {
159-
if (config.isAutoRead()) {
160-
dequeueAll(ctx);
161-
ctx.read();
162-
} else {
164+
if (!config.isAutoRead()) {
163165
unsatisfiedReads++;
166+
}
164167

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-
}
168+
if (!dequeue(ctx) || config.isAutoRead()) {
169+
assert config.isAutoRead() || unsatisfiedReads > 0;
170+
// We either could not satisfy the read or auto-read is on.
171+
// In both cases we need to delegate the read upstream.
172+
ctx.read();
173+
} else if (unsatisfiedReads == 0) {
174+
// Auto-read is off, and we have satisfied all reads.
175+
// As such, we can complete the current read cycle.
176+
ctx.fireChannelReadComplete();
177+
} else {
178+
// Auto-read is off, and unsatisfiedReads is still > 0. dequeue() decrements unsatisfiedReads
179+
// only after fireChannelRead(), so a re-entrant (nested) read sees this read still counted here,
180+
// waits, and leaves the single channelReadComplete to the outermost call. Otherwise, wait for an
181+
// upstream channelRead() or channelReadComplete() to complete the read cycle.
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) {
192195
ctx.fireChannelReadComplete();
193196
}
194197
}
@@ -204,45 +207,46 @@ 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;
217+
private boolean dequeue(ChannelHandlerContext ctx) {
218+
boolean didSatisfyARead = false;
225219

226220
// fireChannelRead(...) may call ctx.read() and so this method may be re-entered. Because of that
227221
// 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)) {
222+
while (queue != null && (config.isAutoRead() || unsatisfiedReads > 0)) {
229223
Object msg = queue.poll();
230224
if (msg == null) {
231225
break;
232226
}
233227

234-
++consumed;
235228
ctx.fireChannelRead(msg);
229+
230+
didSatisfyARead = true;
231+
if (unsatisfiedReads > 0) {
232+
unsatisfiedReads--;
233+
}
234+
235+
if (!config.isAutoRead()) {
236+
// Auto-read is off: deliver a single message per read(). unsatisfiedReads is decremented
237+
// above, after the (possibly re-entrant) fireChannelRead, so a nested read still counts this
238+
// read in unsatisfiedReads and defers completion to the outermost call instead of
239+
// over-draining the queue.
240+
break;
241+
}
236242
}
237243

238244
if (queue != null && queue.isEmpty()) {
239245
queue.recycle();
240246
queue = null;
241247
}
242248

243-
unsatisfiedReads = Math.max(unsatisfiedReads - consumed, 0);
244-
245-
return consumed;
249+
return didSatisfyARead;
246250
}
247251

248252
/**

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.