Class 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
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      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 getConnectedCount()
      Return the current number of connections.
      • 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 Detail

      • ConnectionControlHandler

        public ConnectionControlHandler​(@Nonnegative
                                        int connectionLimit)
      • ConnectionControlHandler

        public ConnectionControlHandler​(@Nonnull
                                        java.util.function.IntSupplier connectionLimit)
    • Method Detail

      • channelActive

        public void channelActive​(io.netty.channel.ChannelHandlerContext ctx)
                           throws java.lang.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:
        java.lang.Exception
      • channelInactive

        public void channelInactive​(io.netty.channel.ChannelHandlerContext ctx)
                             throws java.lang.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:
        java.lang.Exception