Class ConnectionControlHandler
java.lang.Object
io.netty.channel.ChannelHandlerAdapter
io.netty.channel.ChannelInboundHandlerAdapter
com.linkedin.alpini.netty4.handlers.ConnectionLimitHandler
com.linkedin.alpini.netty4.handlers.ConnectionControlHandler
- All Implemented Interfaces:
io.netty.channel.ChannelHandler
,io.netty.channel.ChannelInboundHandler
A
ChannelHandler
which regulates the number of open connections by disabling
the parent socket auto-read config when the number of active connections
exceeds the limit value.
This avoids the nasty case where a client creates a connection and then expects
to be able to use a newly opened connection.
When a flood of new connections are created, the actual number of active
connections may exceed the limit value; this may be controlled by setting
the SO_BACKLOG
value on the server socket but due to thread context
switching, some overshoot is inevitable.
If the connection limit value is set to a value less than 1, it is possible to
get into a state where there are no connections and the server auto-read
is false
. If it is desirable to be able to have no connections, a
periodic task enabling auto-read on the server channel is advisable.-
Nested Class Summary
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
io.netty.channel.ChannelHandler.Sharable
-
Constructor Summary
ConstructorDescriptionConnectionControlHandler
(int connectionLimit) ConnectionControlHandler
(IntSupplier connectionLimit) -
Method Summary
Modifier and TypeMethodDescriptionvoid
channelActive
(io.netty.channel.ChannelHandlerContext ctx) CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.void
channelInactive
(io.netty.channel.ChannelHandlerContext ctx) CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.int
Return the current number of connections.Methods inherited from class com.linkedin.alpini.netty4.handlers.ConnectionLimitHandler
getConnectionLimit, setConnectionLimit
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRead, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggered
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Constructor Details
-
ConnectionControlHandler
public ConnectionControlHandler(@Nonnegative int connectionLimit) -
ConnectionControlHandler
-
-
Method Details
-
channelActive
Description copied from class:ConnectionLimitHandler
CallsChannelHandlerContext.fireChannelActive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.Sub-classes may override this method to change behavior.
- Specified by:
channelActive
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelActive
in classConnectionLimitHandler
- Throws:
Exception
-
channelInactive
Description copied from class:ConnectionLimitHandler
CallsChannelHandlerContext.fireChannelInactive()
to forward to the nextChannelInboundHandler
in theChannelPipeline
.Sub-classes may override this method to change behavior.
- Specified by:
channelInactive
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelInactive
in classConnectionLimitHandler
- Throws:
Exception
-
getConnectedCount
public int getConnectedCount()Description copied from class:ConnectionLimitHandler
Return the current number of connections.- Overrides:
getConnectedCount
in classConnectionLimitHandler
- Returns:
- number of connections.
-