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 -
Field Summary
Fields inherited from class com.linkedin.alpini.netty4.handlers.ConnectionLimitHandler
_connectionCountRecorder, _rejectedConnectionCountRecorder -
Constructor Summary
ConstructorsConstructorDescriptionConnectionControlHandler(IntSupplier connectionLimit, Consumer<Integer> connectionCountRecorder, Consumer<Integer> rejectedConnectionCountRecorder) -
Method Summary
Modifier and TypeMethodDescriptionvoidchannelActive(io.netty.channel.ChannelHandlerContext ctx) CallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.voidchannelInactive(io.netty.channel.ChannelHandlerContext ctx) CallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.intReturn the current number of connections.Methods inherited from class com.linkedin.alpini.netty4.handlers.ConnectionLimitHandler
getConnectionLimit, setConnectionLimitMethods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelRead, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught, userEventTriggeredMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
handlerAdded, handlerRemoved
-
Constructor Details
-
ConnectionControlHandler
public ConnectionControlHandler(@Nonnull IntSupplier connectionLimit, Consumer<Integer> connectionCountRecorder, Consumer<Integer> rejectedConnectionCountRecorder)
-
-
Method Details
-
channelActive
Description copied from class:ConnectionLimitHandlerCallsChannelHandlerContext.fireChannelActive()to forward to the nextChannelInboundHandlerin theChannelPipeline.Sub-classes may override this method to change behavior.
- Specified by:
channelActivein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelActivein classConnectionLimitHandler- Throws:
Exception
-
channelInactive
Description copied from class:ConnectionLimitHandlerCallsChannelHandlerContext.fireChannelInactive()to forward to the nextChannelInboundHandlerin theChannelPipeline.Sub-classes may override this method to change behavior.
- Specified by:
channelInactivein interfaceio.netty.channel.ChannelInboundHandler- Overrides:
channelInactivein classConnectionLimitHandler- Throws:
Exception
-
getConnectedCount
public int getConnectedCount()Description copied from class:ConnectionLimitHandlerReturn the current number of connections.- Overrides:
getConnectedCountin classConnectionLimitHandler- Returns:
- number of connections.
-