Module stormpot

Class PoolBuilderImpl<T extends Poolable>

java.lang.Object
stormpot.internal.PoolBuilderImpl<T>
Type Parameters:
T - The concrete poolable type.
All Implemented Interfaces:
Cloneable, PoolBuilder<T>

public final class PoolBuilderImpl<T extends Poolable> extends Object implements PoolBuilder<T>
The PoolBuilder implementation.
  • Field Details

  • Constructor Details

    • PoolBuilderImpl

      public PoolBuilderImpl(AllocationProcess allocationProcess, Allocator<T> allocator)
      Build a new empty PoolBuilder object.
      Parameters:
      allocationProcess - The allocation process to use.
      allocator - The allocator instance to use.
  • Method Details

    • setSize

      public PoolBuilder<T> setSize(long size)
      Description copied from interface: PoolBuilder
      Set the size of the pool we are building.

      Pools are required to control the allocations and deallocations, such that no more than this number of objects are allocated at any time.

      This means that a pool of size 1, whose single object have expired, will deallocate that one object before allocating a replacement.

      The size must be at least zero, or an IllegalArgumentException will be thrown when building the pool.

      Note that the pool size can be modified after the pool has been built, by calling the Pool.setTargetSize(long) or ManagedPool.setTargetSize(long) methods.

      Specified by:
      setSize in interface PoolBuilder<T extends Poolable>
      Parameters:
      size - The target pool size. Must be at least 0.
      Returns:
      This PoolBuilder instance.
    • getSize

      public long getSize()
      Description copied from interface: PoolBuilder
      Get the currently configured size. The default is 10.
      Specified by:
      getSize in interface PoolBuilder<T extends Poolable>
      Returns:
      The configured pool size.
    • setAllocator

      public <X extends Poolable> PoolBuilder<X> setAllocator(Allocator<X> allocator)
      Description copied from interface: PoolBuilder
      Set the Allocator or Reallocator to use for the pools we want to configure. This will change the type-parameter of the PoolBuilder object to match that of the new Allocator.

      The allocator is initially specified by the Pool.from(Allocator) method, so there is usually no need to set it later.

      Specified by:
      setAllocator in interface PoolBuilder<T extends Poolable>
      Type Parameters:
      X - The type of Poolable that is created by the allocator, and the type of objects that the configured pools will contain.
      Parameters:
      allocator - The allocator we want our pools to use. This cannot be null.
      Returns:
      This PoolBuilder instance, but with a generic type parameter that matches that of the allocator.
    • getAllocator

      public Allocator<T> getAllocator()
      Description copied from interface: PoolBuilder
      Get the configured Allocator instance.
      Specified by:
      getAllocator in interface PoolBuilder<T extends Poolable>
      Returns:
      The configured Allocator instance.
    • getReallocator

      public Reallocator<T> getReallocator()
      Description copied from interface: PoolBuilder
      Get the configured Allocator instance as a Reallocator. If the configured allocator implements the Reallocator interface, then it is returned directly. Otherwise, the allocator is wrapped in an adaptor.
      Specified by:
      getReallocator in interface PoolBuilder<T extends Poolable>
      Returns:
      A configured or adapted Reallocator.
    • setExpiration

      public PoolBuilder<T> setExpiration(Expiration<? super T> expiration)
      Description copied from interface: PoolBuilder
      Set the Expiration to use for the pools we want to configure. The Expiration determines when a pooled object is valid for claiming, or when the objects are invalid and should be deallocated.

      The default Expiration is an Expiration.after(long, long, TimeUnit) that invalidates the objects after they have been active for somewhere from 8 to 10 minutes.

      Specified by:
      setExpiration in interface PoolBuilder<T extends Poolable>
      Parameters:
      expiration - The expiration we want our pools to use. Not null.
      Returns:
      This PoolBuilder instance.
    • getExpiration

      public Expiration<? super T> getExpiration()
      Description copied from interface: PoolBuilder
      Get the configured Expiration instance. The default is a Expiration.after(long, long, TimeUnit) that expires objects after somewhere from 8 to 10 minutes.
      Specified by:
      getExpiration in interface PoolBuilder<T extends Poolable>
      Returns:
      The configured Expiration.
    • setMetricsRecorder

      public PoolBuilder<T> setMetricsRecorder(MetricsRecorder metricsRecorder)
      Description copied from interface: PoolBuilder
      Set the MetricsRecorder to use for the pools we want to configure.
      Specified by:
      setMetricsRecorder in interface PoolBuilder<T extends Poolable>
      Parameters:
      metricsRecorder - The MetricsRecorder to use, or null if we don't want to use any.
      Returns:
      This PoolBuilder instance.
    • getMetricsRecorder

      public MetricsRecorder getMetricsRecorder()
      Description copied from interface: PoolBuilder
      Get the configured MetricsRecorder instance, or null if none has been configured.
      Specified by:
      getMetricsRecorder in interface PoolBuilder<T extends Poolable>
      Returns:
      The configured MetricsRecorder.
    • getThreadFactory

      public ThreadFactory getThreadFactory()
      Description copied from interface: PoolBuilder
      Get the ThreadFactory that has been configured, and will be used to create the background allocation threads for the pools. The default is similar to the Executors.defaultThreadFactory(), except the string "Stormpot-" is prepended to the thread name.
      Specified by:
      getThreadFactory in interface PoolBuilder<T extends Poolable>
      Returns:
      The configured thread factory.
    • setThreadFactory

      public PoolBuilder<T> setThreadFactory(ThreadFactory factory)
      Description copied from interface: PoolBuilder
      Set the ThreadFactory that the pools will use to create its background threads with. The ThreadFactory is not allowed to be null, and creating a pool with a null ThreadFactory will throw an IllegalArgumentException.
      Specified by:
      setThreadFactory in interface PoolBuilder<T extends Poolable>
      Parameters:
      factory - The ThreadFactory the pool should use to create their background threads.
      Returns:
      This PoolBuilder instance.
    • isPreciseLeakDetectionEnabled

      public boolean isPreciseLeakDetectionEnabled()
      Description copied from interface: PoolBuilder
      Return whether precise object leak detection is enabled, which is the case by default.
      Specified by:
      isPreciseLeakDetectionEnabled in interface PoolBuilder<T extends Poolable>
      Returns:
      true if precise object leak detection is enabled.
      See Also:
    • setPreciseLeakDetectionEnabled

      public PoolBuilder<T> setPreciseLeakDetectionEnabled(boolean enabled)
      Description copied from interface: PoolBuilder
      Enable or disable precise object leak detection. It is enabled by default. Precise object leak detection makes the pool keep an eye on the allocated Poolables, such that it notices if they get garbage collected without first being deallocated. Using the garbage collector for this purpose, means that no false positives (counting objects as leaked, even though they are not) are ever reported.
      Note
      NOTE While the pool is able to detect object leaks, it cannot prevent them. All leaks are a sign that there is a bug in the system; most likely a bug in your code, or in the way the pool is used.
      Precise object leak detection incurs virtually no overhead, and is safe to leave enabled at all times – even in the most demanding production environments.
      Specified by:
      setPreciseLeakDetectionEnabled in interface PoolBuilder<T extends Poolable>
      Parameters:
      enabled - true to turn on precise object leak detection (the default) false to turn it off.
      Returns:
      This PoolBuilder instance.
    • isBackgroundExpirationEnabled

      public boolean isBackgroundExpirationEnabled()
      Description copied from interface: PoolBuilder
      Return whether background expiration is enabled. By default, background expiration is enabled.
      Specified by:
      isBackgroundExpirationEnabled in interface PoolBuilder<T extends Poolable>
      Returns:
      true if background expiration is enabled.
      See Also:
    • setBackgroundExpirationEnabled

      public PoolBuilder<T> setBackgroundExpirationEnabled(boolean enabled)
      Description copied from interface: PoolBuilder
      Enable or disable background object expiration checking. This is enabled by default, but can be turned off if the check is expensive. The cost of the check matters because it might end up taking resources away from the background thread and hinder its ability to keep up with the demand for allocations and deallocations, even though these tasks always take priority over any expiration checking.
      Specified by:
      setBackgroundExpirationEnabled in interface PoolBuilder<T extends Poolable>
      Parameters:
      enabled - true (the default) to turn background expiration checking on, false to turn it off.
      Returns:
      This PoolBuilder instance.
    • getBackgroundExpirationCheckDelay

      public int getBackgroundExpirationCheckDelay()
      Description copied from interface: PoolBuilder
      Return the default approximate delay, in milliseconds, between background maintenance tasks, such as the background expiration checks and retrying failed allocations.
      Specified by:
      getBackgroundExpirationCheckDelay in interface PoolBuilder<T extends Poolable>
      Returns:
      the delay, in milliseconds, between background maintenance tasks.
    • setBackgroundExpirationCheckDelay

      public PoolBuilder<T> setBackgroundExpirationCheckDelay(int delay)
      Description copied from interface: PoolBuilder
      Set the approximate delay, in milliseconds, between background maintenance tasks. These tasks include the background expiration checks, and retrying failed allocations.

      The default delay is 1.000 milliseconds (1 second). Lowering this value will improve the pools responsiveness to repairing failed allocations, and also increase the frequency of the background expiration checks. This comes at the cost of higher idle CPU usage.

      It is not recommended to set this value lower than 100 milliseconds. Values lower than this tend to have increased CPU and power usage, for very little gain in responsiveness for the background tasks.

      Specified by:
      setBackgroundExpirationCheckDelay in interface PoolBuilder<T extends Poolable>
      Parameters:
      delay - the desired delay, in milliseconds, between background maintenance tasks.
      Returns:
      This PoolBuilder instance.
    • isOptimizeForReducedMemoryUsage

      public boolean isOptimizeForReducedMemoryUsage()
      Description copied from interface: PoolBuilder
      Return whether Stormpot will prioritize minimizing its memory overhead over maximizing performance.

      This is true by default, as the performance gains don't show except in intense and highly concurrent use cases.

      Specified by:
      isOptimizeForReducedMemoryUsage in interface PoolBuilder<T extends Poolable>
      Returns:
      true for prioritizing memory usage over absolute performance, otherwise false for prioritizing performance at all costs.
    • setOptimizeForReducedMemoryUsage

      public PoolBuilder<T> setOptimizeForReducedMemoryUsage(boolean reduceMemoryUsage)
      Description copied from interface: PoolBuilder
      Tell the pool to optimize for either low memory usage (when giving true), or maximal performance (when giving false).

      This is true by default, and should only be set to false when the pool is expected to be "relatively small" and will experience an extremely high level of multithreaded access.

      Specified by:
      setOptimizeForReducedMemoryUsage in interface PoolBuilder<T extends Poolable>
      Parameters:
      reduceMemoryUsage - whether to prioritize memory usage or performance.
      Returns:
      This PoolBuilder instance.
    • clone

      public PoolBuilderImpl<T> clone()
      Description copied from interface: PoolBuilder
      Returns a shallow copy of this PoolBuilder object.
      Specified by:
      clone in interface PoolBuilder<T extends Poolable>
      Overrides:
      clone in class Object
      Returns:
      A new PoolBuilder object of the exact same type as this one, with identical values in all its fields.
    • build

      public Pool<T> build()
      Description copied from interface: PoolBuilder
      Build a Pool instance based on the collected configuration.
      Specified by:
      build in interface PoolBuilder<T extends Poolable>
      Returns:
      A Pool instance as configured by this builder.
    • getAdaptedReallocator

      public Reallocator<T> getAdaptedReallocator()
      Get a Reallocator from this pool builder, possibly by adapting the configured Allocator.
      Returns:
      A Reallocator instance, either the one given to the pool builder, or a new adapted instance.