Class NettyChannelPool

  • All Implemented Interfaces:
    Pool<io.netty.channel.Channel>, java.io.Closeable, java.lang.AutoCloseable

    @ThreadSafe
    public final class NettyChannelPool
    extends DynamicResourcePool<io.netty.channel.Channel>
    A pool to manage netty channels. Netty has it own implementation of channel pool but that doesn't fit our needs for several reasons: 1. We need a dynamic pool which can garbage collect idle channels. 2. We need to have control on how channel is created. For example, our channel handler might not be annotated with Sharable. So we need to deep copy handlers when creating new channels. 3. Netty channel pool interface is async which is not necessary for our usecase.
    • Constructor Detail

      • NettyChannelPool

        public NettyChannelPool​(io.netty.bootstrap.Bootstrap bootstrap,
                                int maxCapacity,
                                long gcThresholdMs)
        Creates a netty channel pool instance with a minimum capacity of 1.
        Parameters:
        bootstrap - the netty bootstrap used to create netty channel
        maxCapacity - the maximum capacity of the pool
        gcThresholdMs - when a channel is older than this threshold and the pool's capacity is above the minimum capacity(1), it is closed and removed from the pool.
    • Method Detail

      • closeResource

        protected void closeResource​(io.netty.channel.Channel channel)
        Description copied from class: DynamicResourcePool
        Closes the resource. After this, the resource should not be used. It is not guaranteed that the resource is closed after the function returns.
        Specified by:
        closeResource in class DynamicResourcePool<io.netty.channel.Channel>
        Parameters:
        channel - the resource to close
      • createNewResource

        protected io.netty.channel.Channel createNewResource()
                                                      throws java.io.IOException
        Creates a netty channel instance.
        Specified by:
        createNewResource in class DynamicResourcePool<io.netty.channel.Channel>
        Returns:
        the channel created
        Throws:
        java.io.IOException
      • isHealthy

        protected boolean isHealthy​(io.netty.channel.Channel channel)
        Checks whether a channel is healthy.
        Specified by:
        isHealthy in class DynamicResourcePool<io.netty.channel.Channel>
        Parameters:
        channel - the channel to check
        Returns:
        true if the channel is active (i.e. connected)
      • shouldGc

        protected boolean shouldGc​(DynamicResourcePool.ResourceInternal<io.netty.channel.Channel> channelResourceInternal)
        Specified by:
        shouldGc in class DynamicResourcePool<io.netty.channel.Channel>
        Parameters:
        channelResourceInternal - the resource to check
        Returns:
        true if the resource should be garbage collected