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
@Sharable public class ConnectionControlHandler extends ConnectionLimitHandler
AChannelHandler
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 theSO_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 isfalse
. If it is desirable to be able to have no connections, a periodic task enabling auto-read on the server channel is advisable.
-
-
Constructor Summary
Constructors Constructor Description ConnectionControlHandler(int connectionLimit)
ConnectionControlHandler(java.util.function.IntSupplier connectionLimit)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
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
getConnectedCount()
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
-
-
-
-
Method Detail
-
channelActive
public void channelActive(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
channelInactive
public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws java.lang.Exception
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:
java.lang.Exception
-
getConnectedCount
public int getConnectedCount()
Description copied from class:ConnectionLimitHandler
Return the current number of connections.- Overrides:
getConnectedCount
in classConnectionLimitHandler
- Returns:
- number of connections.
-
-