Class DynamicResourcePool<T>

  • Type Parameters:
    T - the type of the resource
    All Implemented Interfaces:
    Pool<T>, java.io.Closeable, java.lang.AutoCloseable
    Direct Known Subclasses:
    NettyChannelPool

    @ThreadSafe
    public abstract class DynamicResourcePool<T>
    extends java.lang.Object
    implements Pool<T>
    A dynamic pool that manages the resources. It clears old resources. It accepts a min and max capacity. When acquiring resources, the most recently used resource is returned.
    • Field Detail

      • mClock

        protected java.time.Clock mClock
    • Constructor Detail

      • DynamicResourcePool

        public DynamicResourcePool​(DynamicResourcePool.Options options)
        Creates a dynamic pool instance.
        Parameters:
        options - the options
    • Method Detail

      • getMetricCounter

        protected abstract com.codahale.metrics.Counter getMetricCounter()
      • acquire

        public T acquire()
                  throws java.io.IOException
        Acquires a resource of type {code T} from the pool.
        Specified by:
        acquire in interface Pool<T>
        Returns:
        the acquired resource
        Throws:
        java.io.IOException
      • acquire

        public T acquire​(long time,
                         java.util.concurrent.TimeUnit unit)
                  throws java.util.concurrent.TimeoutException,
                         java.io.IOException
        Acquires a resource of type {code T} from the pool. This method is like acquire(), but it will time out if an object cannot be acquired before the specified amount of time.
        Specified by:
        acquire in interface Pool<T>
        Parameters:
        time - an amount of time to wait
        unit - the unit to use for time
        Returns:
        a resource taken from the pool
        Throws:
        java.util.concurrent.TimeoutException - if it fails to acquire because of time out
        java.io.IOException - if the thread is interrupted while acquiring the resource
      • release

        public void release​(T resource)
        Releases the resource to the pool. It expects the resource to be released was acquired from this pool. release(Object) and acquire() must be paired. Do not release the resource acquired multiple times. The behavior is undefined if that happens.
        Specified by:
        release in interface Pool<T>
        Parameters:
        resource - the resource to release
      • close

        public void close()
                   throws java.io.IOException
        Closes the pool and clears all the resources. The resource pool should not be used after this.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • size

        public int size()
        Specified by:
        size in interface Pool<T>
        Returns:
        the current pool size
      • shouldGc

        protected abstract boolean shouldGc​(DynamicResourcePool.ResourceInternal<T> resourceInternal)
        Parameters:
        resourceInternal - the resource to check
        Returns:
        true if the resource should be garbage collected
      • isHealthy

        protected abstract boolean isHealthy​(T resource)
        Checks whether a resource is healthy or not.
        Parameters:
        resource - the resource to check
        Returns:
        true if the resource is healthy
      • closeResource

        protected abstract void closeResource​(T resource)
                                       throws java.io.IOException
        Closes the resource. After this, the resource should not be used. It is not guaranteed that the resource is closed after the function returns.
        Parameters:
        resource - the resource to close
        Throws:
        java.io.IOException
      • createNewResource

        protected abstract T createNewResource()
                                        throws java.io.IOException
        Creates a new resource.
        Returns:
        the newly created resource
        Throws:
        java.io.IOException