java.lang.Object
io.lettuce.core.support.BasePool
io.lettuce.core.support.BoundedAsyncPool<T>
- All Implemented Interfaces:
AsyncCloseable,AsyncPool<T>,Closeable,AutoCloseable
public class BoundedAsyncPool<T> extends BasePool implements AsyncPool<T>
Bounded asynchronous object pool. This object pool allows pre-warming with
idle
objects upon construction. The pool is stateful and requires cleanup once it's no longer in use.
Object pool bounds are maintained on a best-effort basis as bounds are maintained upon object request whereas the actual object creation might finish at a later time. You might see temporarily slight differences in object usage vs. pool count due to asynchronous processing vs. protecting the pool from exceed its bounds.
- Since:
- 5.1
- Author:
- Mark Paluch
- See Also:
BoundedPoolConfig,AsyncObjectFactory
-
Field Summary
Fields Modifier and Type Field Description static CompletableFuture<Object>COMPLETED_FUTURE -
Constructor Summary
Constructors Constructor Description BoundedAsyncPool(AsyncObjectFactory<T> factory, BoundedPoolConfig poolConfig) -
Method Summary
Modifier and Type Method Description CompletableFuture<T>acquire()Acquire an object from thisAsyncPool.voidclear()Clear the pool.CompletableFuture<Void>clearAsync()Clear the pool.voidclose()CompletableFuture<Void>closeAsync()Requests to close this object and releases any system resources associated with it.static <T> CompletionStage<BoundedAsyncPool<T>>create(AsyncObjectFactory<T> factory, BoundedPoolConfig poolConfig)Create and initializeBoundedAsyncPoolasynchronously.intgetCreationInProgress()intgetIdle()intgetMaxIdle()Returns the cap on the number of "idle" instances in the pool.intgetMaxTotal()Returns the maximum number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time.intgetMinIdle()Returns the target for the minimum number of idle objects to maintain in the pool.intgetObjectCount()CompletableFuture<Void>release(T object)Release an object back to thisAsyncPool.Methods inherited from class io.lettuce.core.support.BasePool
isTestOnAcquire, isTestOnCreate, isTestOnRelease
-
Field Details
-
COMPLETED_FUTURE
-
-
Constructor Details
-
BoundedAsyncPool
Create a newBoundedAsyncPoolgivenBasePoolConfigandAsyncObjectFactory. The factory creates idle objects upon construction and requiresterminationonce it's no longer in use.Please note that pre-initialization cannot be awaited when using this constructor. Please use
create(AsyncObjectFactory, BoundedPoolConfig)instead.- Parameters:
factory- must not benull.poolConfig- must not benull.
-
-
Method Details
-
create
public static <T> CompletionStage<BoundedAsyncPool<T>> create(AsyncObjectFactory<T> factory, BoundedPoolConfig poolConfig)Create and initializeBoundedAsyncPoolasynchronously.- Type Parameters:
T- object type that is managed by the pool.- Parameters:
factory- must not benull.poolConfig- must not benull.- Returns:
- a
CompletionStagethat completes with theBoundedAsyncPoolwhen created and pre-initialized successfully. Completes exceptionally if the pool initialization failed. - Since:
- 5.3.3
-
acquire
Description copied from interface:AsyncPoolAcquire an object from thisAsyncPool. The returnedCompletableFutureis notified once the acquire is successful and failed otherwise. Behavior upon acquiring objects from an exhausted pool depends on the actual pool implementation whether requests are rejected immediately (exceptional completion withNoSuchElementException) or delayed after exceeding a particular timeout (TimeoutException). It's required that an acquired object is always released to the pool again once the object is no longer in use.. -
release
Description copied from interface:AsyncPoolRelease an object back to thisAsyncPool. The returnedCompletableFutureis notified once the release is successful and failed otherwise. When failed the object will automatically disposed. -
clear
public void clear()Description copied from interface:AsyncPoolClear the pool. -
clearAsync
Description copied from interface:AsyncPoolClear the pool.- Specified by:
clearAsyncin interfaceAsyncPool<T>
-
close
public void close() -
closeAsync
Description copied from interface:AsyncCloseableRequests to close this object and releases any system resources associated with it. If the object is already closed then invoking this method has no effect.Calls to this method return a
CompletableFuturethat is notified with the outcome of the close request.- Specified by:
closeAsyncin interfaceAsyncCloseable- Specified by:
closeAsyncin interfaceAsyncPool<T>
-
getMaxTotal
public int getMaxTotal()Returns the maximum number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. When negative, there is no limit to the number of objects that can be managed by the pool at one time.- Returns:
- the cap on the total number of object instances managed by the pool.
- See Also:
BoundedPoolConfig.getMaxTotal()
-
getMaxIdle
public int getMaxIdle()Returns the cap on the number of "idle" instances in the pool. IfmaxIdleis set too low on heavily loaded systems it is possible you will see objects being destroyed and almost immediately new objects being created. This is a result of the active threads momentarily returning objects faster than they are requesting them them, causing the number of idle objects to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.- Returns:
- the maximum number of "idle" instances that can be held in the pool.
- See Also:
BoundedPoolConfig.getMaxIdle()
-
getMinIdle
public int getMinIdle()Returns the target for the minimum number of idle objects to maintain in the pool. If this is the case, an attempt is made to ensure that the pool has the required minimum number of instances during idle object eviction runs.If the configured value of minIdle is greater than the configured value for
maxIdlethen the value ofmaxIdlewill be used instead.- Returns:
- The minimum number of objects.
- See Also:
BoundedPoolConfig.getMinIdle()
-
getIdle
public int getIdle() -
getObjectCount
public int getObjectCount() -
getCreationInProgress
public int getCreationInProgress()
-