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().setChannelOption(ChannelOption.AUTO_READ, 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.
}
}
}
ChannelOption#AUTO_READ)
| Modifier and Type | Class and Description |
|---|---|
| private static class | FlowControlHandler.RecyclableArrayDeque
A recyclable |
| Modifier and Type | Field and Description |
|---|---|
| private static final InternalLogger | |
| private FlowControlHandler. | |
| private final boolean | |
| private boolean |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | channelInactive(ChannelHandlerContext ctx)
Overrides default io. Channel of the ChannelHandlerContext was registered is now inactive and reached its
end of lifetime.
|
| public void | channelRead(ChannelHandlerContext ctx, Object msg)
Overrides default io. Channel has read a message from the peer.
|
| public void | channelReadComplete(ChannelHandlerContext ctx)
Overrides default io. channelRead(ChannelHandlerContext, Object).
|
| 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. |
| private void | |
| public void | handlerRemoved(ChannelHandlerContext ctx)
Overrides default io. ChannelHandler was removed from the actual context and it doesn't handle events
anymore.
|
| pack-priv boolean | |
| public void | read(ChannelHandlerContext
the ctx, ReadBufferAllocator ChannelHandlerContext for which the read operation is madeThe readBufferAllocator)ReadBufferAllocator that should be used to allocate a Buffer
if needed (for reading the data).Overrides default io. EventLoop.
|
| 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 default io. Doc from io. The
| |
| channelRead | back to summary |
|---|---|
| public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception Overrides default io. Doc from io. Invoked when the current
| |
| channelReadComplete | back to summary |
|---|---|
| public void channelReadComplete(ChannelHandlerContext ctx) throws Exception Overrides default io. Doc from io. Invoked when the last message read by the current read operation has been consumed by
| |
| 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 | |
| handlerRemoved | back to summary |
|---|---|
| public void handlerRemoved(ChannelHandlerContext ctx) throws Exception Overrides default 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, ReadBufferAllocator readBufferAllocator) Overrides default io. Doc from io. Called once a read operation is made from the current registered
| |
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() | |