Top Description Inners Fields Constructors Methods
reactor.pool.decorators

public final Class GracefulShutdownInstrumentedPool<T>

extends Object
implements InstrumentedPool<T>
Class Inheritance
All Implemented Interfaces
reactor.pool.InstrumentedPool, reactor.pool.Pool, reactor.core.Disposable
Imports
java.time.Duration, java.util.Objects, java.util.concurrent.TimeUnit, .TimeoutException, java.util.concurrent.atomic.AtomicBoolean, .AtomicInteger, .AtomicLong, reactor.core.Disposable, .Disposables, reactor.core.publisher.Mono, .SignalType, .Sinks, reactor.core.scheduler.Scheduler, .Schedulers, reactor.pool.InstrumentedPool, .Pool, .PoolConfig, .PoolShutdownException, .PooledRef, .PooledRefMetadata, reactor.util.Logger, .Loggers

A decorating 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 and Inner Type Summary

Modifier and TypeClass and Description
pack-priv class

Field Summary

Modifier and TypeField and Description
pack-priv final AtomicLong
pack-priv final Sinks.Empty<Void>
pack-priv final AtomicInteger
private static final Logger
pack-priv final InstrumentedPool<T>
pack-priv Disposable
pack-priv final Scheduler

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
public Mono<PooledRef<T>>
acquire()

Implements reactor.pool.Pool.acquire.

Manually acquire a POOLABLE from the pool upon subscription and become responsible for its release.
public Mono<PooledRef<T>>
acquire(Duration timeout)

Implements reactor.pool.Pool.acquire.

Manually acquire a POOLABLE from the pool upon subscription and become responsible for its release.
public PoolConfig<T>
config()

Implements reactor.pool.Pool.config.

Return the pool's configuration.
public Mono<Void>

Returns:

a Mono representing the current graceful shutdown of the pool
disposeGracefully
(final Duration
the maximum Duration for graceful shutdown before full shutdown is forced (resolution: ms)
gracefulTimeout
)

Trigger a "graceful shutdown" of the pool, with a grace period timeout.

public Mono<Void>
disposeLater()

Implements reactor.pool.Pool.disposeLater.

Returns a Mono that represents a lazy asynchronous shutdown of this Pool.
public InstrumentedPool<T>

Returns:

the original decorated InstrumentedPool
getOriginalPool
()

Return the original pool.

public boolean
isDisposed()

Overrides default reactor.core.Disposable.isDisposed.

Optionally return true when the resource or task is disposed.
public boolean

Returns:

true if the pool is in the process of shutting down gracefully, or has already done so
isGracefullyShuttingDown
()

Check if the disposeGracefully(Duration) has been invoked.

public boolean

Returns:

true if the graceful shutdown is still within the grace period, false otherwise
isInGracePeriod
()

Check if the disposeGracefully(Duration) has been invoked but there are still pending acquire and the grace period hasn't timed out.

public InstrumentedPool.PoolMetrics
private Mono<Void>
public Mono<Integer>
warmup()

Implements reactor.pool.Pool.warmup.

Warms up the Pool, if needed.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

acquireTrackerback to summary
pack-priv final AtomicLong acquireTracker
gracefulNotifierback to summary
pack-priv final Sinks.Empty<Void> gracefulNotifier
isGracefulShutdownback to summary
pack-priv final AtomicInteger isGracefulShutdown
LOGGERback to summary
private static final Logger LOGGER
originalPoolback to summary
pack-priv final InstrumentedPool<T> originalPool
timeoutback to summary
pack-priv Disposable timeout
timeoutSchedulerback to summary
pack-priv final Scheduler timeoutScheduler

Constructor Detail

GracefulShutdownInstrumentedPoolback to summary
pack-priv GracefulShutdownInstrumentedPool(InstrumentedPool<T> originalPool)

Method Detail

acquireback to summary
public Mono<PooledRef<T>> acquire()

Implements reactor.pool.Pool.acquire.

Doc from reactor.pool.Pool.acquire.

Manually acquire a POOLABLE from the pool upon subscription and become responsible for its release. The object is wrapped in a PooledRef which 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 PooledRef can be stored. On the other hand, if the resource and its usage directly expose reactive APIs, you might want to prefer to use withPoolable(Function).

The resulting Mono emits the PooledRef as the POOLABLE becomes available. Cancelling the org.reactivestreams.Subscription before the POOLABLE has been emitted will either avoid object acquisition entirely or will translate to a release of the POOLABLE. Once the resource is emitted though, it is the responsibility of the caller to release the poolable object via the PooledRef release methods when 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).

Returns:Mono<PooledRef<T>>

a Mono, each subscription to which represents an individual act of acquiring a pooled object and manually managing its lifecycle from there on

Annotations
@Override
acquireback to summary
public Mono<PooledRef<T>> acquire(Duration timeout)

Implements reactor.pool.Pool.acquire.

Doc from reactor.pool.Pool.acquire.

Manually acquire a POOLABLE from the pool upon subscription and become responsible for its release. The provided Duration acts 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 the Mono#timeout(Duration) operator to the returned Mono. For a timeout that only applies to resource allocation, build the pool with the standard Mono#timeout(Duration) operator chained to the allocator.

The object is wrapped in a PooledRef which 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 PooledRef can be stored. On the other hand, if the resource and its usage directly expose reactive APIs, you might want to prefer to use withPoolable(Function).

The resulting Mono emits the PooledRef as the POOLABLE becomes available. Cancelling the org.reactivestreams.Subscription before the POOLABLE has been emitted will either avoid object acquisition entirely or will translate to a release of the POOLABLE. Once the resource is emitted though, it is the responsibility of the caller to release the poolable object via the PooledRef release methods when 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).

Returns:Mono<PooledRef<T>>

a Mono, each subscription to which represents an individual act of acquiring a pooled object and manually managing its lifecycle from there on

Annotations
@Override
configback to summary
public PoolConfig<T> config()

Implements reactor.pool.Pool.config.

Doc from reactor.pool.Pool.config.

Return the pool's configuration.

Returns:PoolConfig<T>

the PoolConfig

Annotations
@Override
disposeGracefullyback to summary
public Mono<Void> disposeGracefully(final Duration gracefulTimeout)

Trigger a "graceful shutdown" of the pool, with a grace period timeout. From there on, calls to Pool#acquire() and Pool#acquire(Duration) will fail fast with a PoolShutdownException. However, for the provided Duration, pending acquires will get a chance to be served.

If the wrapper detects that all pending acquires are either released or invalidated, the returned Mono will complete successfully. It will do so after having internally called and waited for the original pool's Pool#disposeLater() method, effectively shutting down the pool for good.

If the timeout triggers before that, the returned Mono will also trigger the Pool#disposeLater() method, but will terminate by emitting a TimeoutException. Since it means that at that point some pending acquire are still registered, these are terminated with a PoolShutdownException by the disposeLater() 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 same Mono, 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 via Pool#config()). Otherwise it uses the parallel Scheduler as a fallback.

Parameters
gracefulTimeout:Duration

the maximum Duration for graceful shutdown before full shutdown is forced (resolution: ms)

Returns:Mono<Void>

a Mono representing the current graceful shutdown of the pool

See Also
disposeLater()
disposeLaterback to summary
public Mono<Void> disposeLater()

Implements reactor.pool.Pool.disposeLater.

Doc from reactor.pool.Pool.disposeLater.

Returns a Mono that represents a lazy asynchronous shutdown of this Pool. Shutdown doesn't happen until the Mono is subscribed. Otherwise, it performs the same steps as in the imperative counterpart, dispose().

If the pool has been already shut down, returns Mono#empty(). Completion of the Mono indicates completion of the shutdown process.

Returns:Mono<Void>

a Mono triggering the shutdown of the pool once subscribed.

Annotations
@Override
getOriginalPoolback to summary
public InstrumentedPool<T> 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:InstrumentedPool<T>

the original decorated InstrumentedPool

isDisposedback to summary
public boolean isDisposed()

Overrides default reactor.core.Disposable.isDisposed.

Doc from reactor.core.Disposable.isDisposed.

Optionally return true when the resource or task is disposed.

Implementations are not required to track disposition and as such may never return true even when disposed. However, they MUST only return true when there's a guarantee the resource or task is disposed.

Returns:boolean

true when there's a guarantee the resource or task is disposed.

Annotations
@Override
isGracefullyShuttingDownback to summary
public boolean isGracefullyShuttingDown()

Check if the disposeGracefully(Duration) has been invoked.

Returns:boolean

true if the pool is in the process of shutting down gracefully, or has already done so

isInGracePeriodback to summary
public boolean isInGracePeriod()

Check if the disposeGracefully(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 via disposeLater() (or has already done so).

Returns:boolean

true if the graceful shutdown is still within the grace period, false otherwise

metricsback to summary
public InstrumentedPool.PoolMetrics metrics()

Implements reactor.pool.InstrumentedPool.metrics.

Returns:InstrumentedPool.PoolMetrics

Doc from reactor.pool.InstrumentedPool.metrics.

a PoolMetrics object to be used to get live gauges about the Pool

Annotations
@Override
tryGracefulDoneback to summary
private Mono<Void> tryGracefulDone()
warmupback to summary
public Mono<Integer> warmup()

Implements reactor.pool.Pool.warmup.

Doc from reactor.pool.Pool.warmup.

Warms up the Pool, if needed. This typically instructs the pool to check for a minimum size and allocate necessary objects when the minimum is not reached. The resulting Mono emits the number of extra resources it created as a result of the allocation minimum.

Note that no work nor allocation is performed until the Mono is 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).

Returns:Mono<Integer>

a cold Mono that triggers resource warmup and emits the number of warmed up resources

Annotations
@Override
reactor.pool.decorators back to summary

pack-priv final Class GracefulShutdownInstrumentedPool.GracefulRef

extends AtomicBoolean
implements PooledRef<T>
Class Inheritance
All Implemented Interfaces
reactor.pool.PooledRef

Field Summary

Modifier and TypeField and Description
pack-priv final PooledRef<T>

Constructor Summary

AccessConstructor and Description
public
GracefulRef(PooledRef<T> originalRef)

Method Summary

Modifier and TypeMethod and Description
public Mono<Void>
invalidate()

Implements reactor.pool.PooledRef.invalidate.

Return a Mono that triggers the asynchronous invalidation of the POOLABLE when subscribed.
public PooledRefMetadata
metadata()

Implements reactor.pool.PooledRef.metadata.

Returns a PooledRefMetadata object that holds more information about the reference (rather than the poolable()), like how many times it was acquired, its age (time since allocation), etc...
public T
poolable()

Implements reactor.pool.PooledRef.poolable.

Returns the wrapped POOLABLE.
public Mono<Void>
release()

Implements reactor.pool.PooledRef.release.

Return a Mono that, once subscribed, will release the POOLABLE back to the pool asynchronously.
Inherited from java.util.concurrent.atomic.AtomicBoolean:
compareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetgetgetAcquiregetAndSetgetOpaquegetPlainlazySetsetsetOpaquesetPlainsetReleasetoStringweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Field Detail

originalRefback to summary
pack-priv final PooledRef<T> originalRef

Constructor Detail

GracefulRefback to summary
public GracefulRef(PooledRef<T> originalRef)

Method Detail

invalidateback to summary
public Mono<Void> invalidate()

Implements reactor.pool.PooledRef.invalidate.

Doc from reactor.pool.PooledRef.invalidate.

Return a Mono that triggers the asynchronous invalidation of the POOLABLE when subscribed. The object is always discarded by the pool and not further reused.

This is useful when the unhealthy state of the resource (or lack of re-usability) is detected through the usage of the resource, as opposed to its exposed state.

Note that if release() has been invoked, this method should be no-op.

Annotations
@Override
metadataback to summary
public PooledRefMetadata metadata()

Implements reactor.pool.PooledRef.metadata.

Doc from reactor.pool.PooledRef.metadata.

Returns a PooledRefMetadata object that holds more information about the reference (rather than the poolable()), like how many times it was acquired, its age (time since allocation), etc...

Note that if release() or invalidate() have been invoked, this method might return stale information.

Returns:PooledRefMetadata

the PooledRefMetadata for this PooledRef

Annotations
@Override
poolableback to summary
public T poolable()

Implements reactor.pool.PooledRef.poolable.

Doc from reactor.pool.PooledRef.poolable.

Returns the wrapped POOLABLE. This method is not thread-safe and the poolable should NEVER be accessed concurrently.

The reference is retained by the PooledRef but the object might be in an intermediate or invalid state when one of the release() or invalidate() methods have been previously called.

Returns:T

the poolable

Annotations
@Override
releaseback to summary
public Mono<Void> release()

Implements reactor.pool.PooledRef.release.

Doc from reactor.pool.PooledRef.release.

Return a Mono that, once subscribed, will release the POOLABLE back to the pool asynchronously. Once released, a reference is considered out of the control of the user and shouldn't be usable anymore. It is discouraged to use the underlying poolable() at this point, metadata() might become stale, and usage of both invalidate() and release() should be no-op.

This method is idempotent (subsequent subscriptions to the same Mono, or re-invocations of the method are NO-OP). It is also no-op if invalidate() has previously been invoked.

Returns:Mono<Void>

a Mono that will complete empty when the object has been released. In case of an error the object is always discarded

Annotations
@Override