FlowControlHandler ensures that only one message per read() is sent downstream.
Classes such as ByteToMessageDecoder or MessageToByteEncoder are free to emit as
many events as they like for any given input. A channel's auto reading configuration doesn't usually
apply in these scenarios. This is causing problems in downstream ChannelHandlers that would
like to hold subsequent events while they're processing one event. It's a common problem with the
HttpObjectDecoder that will very often fire an HttpRequest that is immediately followed
by a LastHttpContent event.
ChannelPipeline pipeline = ...;
pipeline.addLast(new HttpServerCodec());
pipeline.addLast(new FlowControlHandler());
pipeline.addLast(new MyExampleHandler());
class MyExampleHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof HttpRequest) {
ctx.channel().config().setAutoRead(false);
// The FlowControlHandler will hold any subsequent events that
// were emitted by HttpObjectDecoder until auto reading is turned
// back on or Channel#read() is being called.
}
}
}
ChannelConfig#setAutoRead(boolean)
| Modifier and Type | Class and Description |
|---|---|
| private static class | FlowControlHandler.
A recyclable |
| Modifier and Type | Field and Description |
|---|---|
| private ChannelConfig | |
| private static final InternalLogger | |
| private FlowControlHandler. | |
| private final boolean | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| config | back to summary |
|---|---|
| private ChannelConfig config | |
| logger | back to summary |
|---|---|
| private static final InternalLogger logger | |
| queue | back to summary |
|---|---|
| private FlowControlHandler. | |
| releaseMessages | back to summary |
|---|---|
| private final boolean releaseMessages | |
| shouldConsume | back to summary |
|---|---|
| private boolean shouldConsume | |
| FlowControlHandler | back to summary |
|---|---|
| public FlowControlHandler() | |
| FlowControlHandler | back to summary |
|---|---|
| public FlowControlHandler(boolean releaseMessages) | |
| channelInactive | back to summary |
|---|---|
| public void channelInactive(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| channelRead | back to summary |
|---|---|
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| channelReadComplete | back to summary |
|---|---|
| public void channelReadComplete(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
| dequeue | back to summary |
|---|---|
| private int dequeue(ChannelHandlerContext ctx, int minConsume) Dequeues one or many (or none) messages depending on the channel's auto
reading state and returns the number of messages that were consumed from
the internal queue.
The | |
| destroy | back to summary |
|---|---|
| private void destroy() Releases all messages and destroys the | |
| handlerAdded | back to summary |
|---|---|
| public void handlerAdded(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Gets called after the
| |
| handlerRemoved | back to summary |
|---|---|
| public void handlerRemoved(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Gets called after the
| |
| isQueueEmpty | back to summary |
|---|---|
| pack-priv boolean isQueueEmpty() Determine if the underlying | |
| read | back to summary |
|---|---|
| public void read(ChannelHandlerContext ctx) throws Exception Overrides io. Implements io. Doc from io. Calls
| |
ArrayDeque.
| Modifier and Type | Field and Description |
|---|---|
| private static final int | DEFAULT_NUM_ELEMENTS
A value of |
| private final ObjectPool. | |
| private static final ObjectPool | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| private | RecyclableArrayDeque(int numElements, ObjectPool.
|
| Modifier and Type | Method and Description |
|---|---|
| public static FlowControlHandler. | |
| public void |
| DEFAULT_NUM_ELEMENTS | back to summary |
|---|---|
| private static final int DEFAULT_NUM_ELEMENTS A value of | |
| handle | back to summary |
|---|---|
| private final ObjectPool. | |
| RECYCLER | back to summary |
|---|---|
| private static final ObjectPool<FlowControlHandler. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID | |
| RecyclableArrayDeque | back to summary |
|---|---|
| private RecyclableArrayDeque(int numElements, ObjectPool. | |
| newInstance | back to summary |
|---|---|
| public static FlowControlHandler. | |
| recycle | back to summary |
|---|---|
| public void recycle() | |