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
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

    Constructors
    Constructor
    Description
    ConnectionControlHandler(int connectionLimit)
     
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    channelActive(io.netty.channel.ChannelHandlerContext ctx)
    Calls ChannelHandlerContext.fireChannelActive() to forward to the next ChannelInboundHandler in the ChannelPipeline.
    void
    channelInactive(io.netty.channel.ChannelHandlerContext ctx)
    Calls ChannelHandlerContext.fireChannelInactive() to forward to the next ChannelInboundHandler in the ChannelPipeline.
    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

      public ConnectionControlHandler(@Nonnull IntSupplier connectionLimit)
  • Method Details

    • channelActive

      public void channelActive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Description copied from class: ConnectionLimitHandler
      Calls ChannelHandlerContext.fireChannelActive() to forward to the next ChannelInboundHandler in the ChannelPipeline.

      Sub-classes may override this method to change behavior.

      Specified by:
      channelActive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelActive in class ConnectionLimitHandler
      Throws:
      Exception
    • channelInactive

      public void channelInactive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
      Description copied from class: ConnectionLimitHandler
      Calls ChannelHandlerContext.fireChannelInactive() to forward to the next ChannelInboundHandler in the ChannelPipeline.

      Sub-classes may override this method to change behavior.

      Specified by:
      channelInactive in interface io.netty.channel.ChannelInboundHandler
      Overrides:
      channelInactive in class ConnectionLimitHandler
      Throws:
      Exception
    • getConnectedCount

      public int getConnectedCount()
      Description copied from class: ConnectionLimitHandler
      Return the current number of connections.
      Overrides:
      getConnectedCount in class ConnectionLimitHandler
      Returns:
      number of connections.