Class GracefulShutdownInstrumentedPool<T>
- All Implemented Interfaces:
Disposable,InstrumentedPool<T>,Pool<T>
InstrumentedPool that adds the capacity to gracefully shut down the pool.
Apply to any InstrumentedPool via the InstrumentedPoolDecorators.gracefulShutdown(InstrumentedPool)
factory method.
Adds the getOriginalPool(), disposeGracefully(Duration), isGracefullyShuttingDown()
and isInGracePeriod() methods.
- Author:
- Simon Baslé
-
Nested Class Summary
Nested classes/interfaces inherited from interface reactor.core.Disposable
Disposable.Composite, Disposable.SwapNested classes/interfaces inherited from interface reactor.pool.InstrumentedPool
InstrumentedPool.PoolMetrics -
Method Summary
Modifier and TypeMethodDescriptionacquire()Manually acquire aPOOLABLEfrom the pool upon subscription and become responsible for its release.Manually acquire aPOOLABLEfrom the pool upon subscription and become responsible for its release.config()Return the pool'sconfiguration.disposeGracefully(Duration gracefulTimeout) Trigger a "graceful shutdown" of the pool, with a grace period timeout.Return the original pool.booleanbooleanCheck if thedisposeGracefully(Duration)has been invoked.booleanCheck if thedisposeGracefully(Duration)has been invoked but there are still pending acquire and the grace period hasn't timed out.metrics()warmup()Warms up thePool, if needed.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface reactor.pool.Pool
dispose, withPoolable
-
Method Details
-
getOriginalPool
Return the original pool. Note that in order for this decorator to work correctly, the original pool MUST NOT be used in conjunction with the decorated pool, so use this method carefully.- Returns:
- the original decorated
InstrumentedPool
-
acquire
Description copied from interface:PoolManually acquire aPOOLABLEfrom the pool upon subscription and become responsible for its release. The object is wrapped in aPooledRefwhich can be used for manually releasing the object back to the pool or invalidating it. As a result, you MUST maintain a reference to it throughout the code that makes use of the underlying resource.This is typically the case when one needs to wrap the actual resource into a decorator version, where the reference to the
PooledRefcan be stored. On the other hand, if the resource and its usage directly expose reactive APIs, you might want to prefer to usePool.withPoolable(Function).The resulting
Monoemits thePooledRefas thePOOLABLEbecomes available. Cancelling theSubscriptionbefore thePOOLABLEhas been emitted will either avoid object acquisition entirely or will translate to areleaseof thePOOLABLE. Once the resource is emitted though, it is the responsibility of the caller to release the poolable object via thePooledRefrelease methodswhen the resource is not used anymore (directly OR indirectly, eg. the results from multiple statements derived from a DB connection type of resource have all been processed). -
acquire
Description copied from interface:PoolManually acquire aPOOLABLEfrom the pool upon subscription and become responsible for its release. The providedDurationacts as a timeout that only applies if the acquisition is added to the pending queue, i.e. there is no idle resource and no new resource can be created currently, so one needs to wait for a release before a resource can be delivered. For a timeout that covers both this pending case and the time it would take to allocate a new resource, simply apply theMono.timeout(Duration)operator to the returned Mono. For a timeout that only applies to resource allocation, build the pool with the standardMono.timeout(Duration)operator chained to theallocator.The object is wrapped in a
PooledRefwhich can be used for manually releasing the object back to the pool or invalidating it. As a result, you MUST maintain a reference to it throughout the code that makes use of the underlying resource.This is typically the case when one needs to wrap the actual resource into a decorator version, where the reference to the
PooledRefcan be stored. On the other hand, if the resource and its usage directly expose reactive APIs, you might want to prefer to usePool.withPoolable(Function).The resulting
Monoemits thePooledRefas thePOOLABLEbecomes available. Cancelling theSubscriptionbefore thePOOLABLEhas been emitted will either avoid object acquisition entirely or will translate to areleaseof thePOOLABLE. Once the resource is emitted though, it is the responsibility of the caller to release the poolable object via thePooledRefrelease methodswhen the resource is not used anymore (directly OR indirectly, eg. the results from multiple statements derived from a DB connection type of resource have all been processed). -
disposeGracefully
Trigger a "graceful shutdown" of the pool, with a grace period timeout. From there on, calls toPool.acquire()andPool.acquire(Duration)will fail fast with aPoolShutdownException. However, for the providedDuration, pending acquires will get a chance to be served.If the wrapper detects that all pending acquires are either
releasedorinvalidated, the returnedMonowill complete successfully. It will do so after having internally called and waited for the original pool'sPool.disposeLater()method, effectively shutting down the pool for good.If the timeout triggers before that, the returned
Monowill also trigger thePool.disposeLater()method, but will terminate by emitting aTimeoutException. Since it means that at that point some pending acquire are still registered, these are terminated with aPoolShutdownExceptionby thedisposeLater()method.Note that the rejection of new acquires and the grace timer start immediately, irrespective of subscription to the returned
Mono. Subsequent calls return the sameMono, effectively getting notifications from the first graceful shutdown call and ignoring subsequently provided timeouts.The timeout runs on the original pool's
PoolConfig.evictInBackgroundScheduler()if it set (and provided the pool correctly exposes its configuration viaPool.config()). Otherwise it uses theparallel Scheduleras a fallback. -
isGracefullyShuttingDown
public boolean isGracefullyShuttingDown()Check if thedisposeGracefully(Duration)has been invoked.- Returns:
- true if the pool is in the process of shutting down gracefully, or has already done so
-
isInGracePeriod
public boolean isInGracePeriod()Check if thedisposeGracefully(Duration)has been invoked but there are still pending acquire and the grace period hasn't timed out.If
isGracefullyShuttingDown()returns true but this method returns false, it means that the pool is now at least in the process of shutting down completely viadisposeLater()(or has already done so).- Returns:
- true if the graceful shutdown is still within the grace period, false otherwise
-
metrics
- Specified by:
metricsin interfaceInstrumentedPool<T>- Returns:
- a
InstrumentedPool.PoolMetricsobject to be used to get live gauges about thePool
-
config
Description copied from interface:PoolReturn the pool'sconfiguration.- Specified by:
configin interfacePool<T>- Returns:
- the
PoolConfig
-
warmup
Description copied from interface:PoolWarms up thePool, if needed. This typically instructs the pool to check for a minimum size and allocate necessary objects when the minimum is not reached. The resultingMonoemits the number of extra resources it created as a result of theallocation minimum.Note that no work nor allocation is performed until the
Monois subscribed to.Implementations MAY include more behavior, but there is no restriction on the way this method is called by users (it should be possible to call it at any time, as many times as needed or not at all).
-
disposeLater
Description copied from interface:PoolReturns aMonothat represents a lazy asynchronous shutdown of thisPool. Shutdown doesn't happen until theMonoissubscribed. Otherwise, it performs the same steps as in the imperative counterpart,Pool.dispose().If the pool has been already shut down, returns
Mono.empty(). Completion of theMonoindicates completion of the shutdown process.- Specified by:
disposeLaterin interfacePool<T>- Returns:
- a Mono triggering the shutdown of the pool once subscribed.
-
isDisposed
public boolean isDisposed()- Specified by:
isDisposedin interfaceDisposable
-