Class RouteSegmentedConnPool<R, C extends ModalCloseable>
java.lang.Object
org.apache.hc.core5.pool.RouteSegmentedConnPool<R,C>
- Type Parameters:
R- route key typeC- connection type (must beModalCloseable)
- All Implemented Interfaces:
Closeable, AutoCloseable, ModalCloseable, ConnPool<R,C>, ConnPoolControl<R>, ConnPoolStats<R>, ManagedConnPool<R, C>
@Contract(threading=SAFE_CONDITIONAL)
@Experimental
public final class RouteSegmentedConnPool<R, C extends ModalCloseable>
extends Object
implements ManagedConnPool<R,C>
Lock-free, route-segmented connection pool with tiny, conditional round-robin assistance.
Per-route state is kept in independent segments. Disposal of connections is offloaded to a bounded executor so slow closes do not block threads leasing on other routes. A minimal round-robin drainer is engaged only when there are many pending routes and there is global headroom; it never scans all routes.
- Since:
- 5.4
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) final class(package private) final class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicBooleanprivate final AtomicIntegerprivate final DisposalCallback<C> private final ThreadPoolExecutorDedicated executor for asynchronous, best-effort disposal.private final AtomicBooleanprivate final ConcurrentHashMap<R, Integer> private final AtomicIntegerprivate final ConcurrentLinkedQueue<R> private final AtomicIntegerprivate final PoolReusePolicyprivate static final intprivate static final intprivate final ConcurrentHashMap<R, RouteSegmentedConnPool<R, C>.Segment> private final ScheduledExecutorServiceprivate final TimeValueprivate final AtomicInteger -
Constructor Summary
ConstructorsConstructorDescriptionRouteSegmentedConnPool(int defaultMaxPerRoute, int maxTotal, TimeValue timeToLive, PoolReusePolicy reusePolicy, DisposalCallback<C> disposal) -
Method Summary
Modifier and TypeMethodDescriptionprivate voidvoidclose()voidCloses this process or endpoint and releases any system resources associated with it.voidvoidprivate booleancompatible(Object needed, Object have) private voidprivate voiddiscardAndDecr(PoolEntry<R, C> p, CloseMode mode) private voiddiscardEntry(PoolEntry<R, C> p, CloseMode preferred) Dispose a pool entry's connection asynchronously if possible; under pressure fall back to IMMEDIATE on caller.private voidenqueueIfNeeded(R route, RouteSegmentedConnPool<R, C>.Segment seg) private voidintintgetMaxPerRoute(R route) intprivate booleanhandOffToCompatibleWaiter(PoolEntry<R, C> entry, RouteSegmentedConnPool<R, C>.Segment seg) private booleanAttempts to lease a connection for the given route and with the given state from the pool.private voidmaybeCleanupSegment(R route, RouteSegmentedConnPool<R, C>.Segment seg) private voidofferAvailable(RouteSegmentedConnPool<R, C>.Segment seg, PoolEntry<R, C> p) private CloseModepollAvailable(RouteSegmentedConnPool<R, C>.Segment seg, Object neededState) voidReleases the pool entry back to the pool.private voidprivate voidserveRoundRobin(int budget) voidsetDefaultMaxPerRoute(int max) voidsetMaxPerRoute(R route, int max) voidsetMaxTotal(int max) private voidprivate booleantryAllocateOne(R route, RouteSegmentedConnPool<R, C>.Segment seg)
-
Field Details
-
RR_MIN_PENDING_ROUTES
private static final int RR_MIN_PENDING_ROUTES- See Also:
-
RR_BUDGET
private static final int RR_BUDGET- See Also:
-
reusePolicy
-
timeToLive
-
disposal
-
defaultMaxPerRoute
-
segments
private final ConcurrentHashMap<R, RouteSegmentedConnPool<R, C extends ModalCloseable>.Segment> segments -
maxPerRoute
-
totalAllocated
-
maxTotal
-
closed
-
timeouts
-
disposer
Dedicated executor for asynchronous, best-effort disposal. Bounded queue; on saturation we fall back to IMMEDIATE close on the caller thread. -
pendingQueue
-
draining
-
pendingRouteCount
-
-
Constructor Details
-
RouteSegmentedConnPool
public RouteSegmentedConnPool(int defaultMaxPerRoute, int maxTotal, TimeValue timeToLive, PoolReusePolicy reusePolicy, DisposalCallback<C> disposal)
-
-
Method Details
-
lease
public Future<PoolEntry<R,C>> lease(R route, Object state, Timeout requestTimeout, FutureCallback<PoolEntry<R, C>> callback) Description copied from interface:ConnPoolAttempts to lease a connection for the given route and with the given state from the pool.Please note the connection request can get automatically cancelled by the pool in case of a request timeout.
- Specified by:
leasein interfaceConnPool<R, C extends ModalCloseable>- Parameters:
route- route of the connection.state- arbitrary object that represents a particular state (usually a security principal or a unique token identifying the user whose credentials have been used while establishing the connection). May benull.requestTimeout- request timeout. In case of a timeout the request can get automatically cancelled by the pool.callback- operation completion callback.- Returns:
- future for a leased pool entry.
-
release
Description copied from interface:ConnPoolReleases the pool entry back to the pool.- Specified by:
releasein interfaceConnPool<R, C extends ModalCloseable>- Parameters:
entry- pool entry leased from the poolreusable- flag indicating whether or not the released connection is in a consistent state and is safe for further use.
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
close
Description copied from interface:ModalCloseableCloses this process or endpoint and releases any system resources associated with it. If the endpoint or the process is already closed then invoking this method has no effect.- Specified by:
closein interfaceModalCloseable- Parameters:
closeMode- How to close the receiver.
-
closeIdle
- Specified by:
closeIdlein interfaceConnPoolControl<R>
-
closeExpired
public void closeExpired()- Specified by:
closeExpiredin interfaceConnPoolControl<R>
-
getRoutes
- Specified by:
getRoutesin interfaceConnPoolControl<R>
-
getMaxTotal
public int getMaxTotal()- Specified by:
getMaxTotalin interfaceConnPoolControl<R>
-
setMaxTotal
public void setMaxTotal(int max) - Specified by:
setMaxTotalin interfaceConnPoolControl<R>
-
getDefaultMaxPerRoute
public int getDefaultMaxPerRoute()- Specified by:
getDefaultMaxPerRoutein interfaceConnPoolControl<R>
-
setDefaultMaxPerRoute
public void setDefaultMaxPerRoute(int max) - Specified by:
setDefaultMaxPerRoutein interfaceConnPoolControl<R>
-
getMaxPerRoute
- Specified by:
getMaxPerRoutein interfaceConnPoolControl<R>
-
setMaxPerRoute
- Specified by:
setMaxPerRoutein interfaceConnPoolControl<R>
-
getTotalStats
- Specified by:
getTotalStatsin interfaceConnPoolStats<R>
-
getStats
- Specified by:
getStatsin interfaceConnPoolStats<R>
-
ensureOpen
private void ensureOpen() -
isPastTtl
-
scheduleTimeout
private void scheduleTimeout(RouteSegmentedConnPool<R, C>.Waiter w, RouteSegmentedConnPool<R, C>.Segment seg) -
cancelTimeout
-
offerAvailable
-
pollAvailable
-
compatible
-
handOffToCompatibleWaiter
-
discardAndDecr
-
orImmediate
-
maybeCleanupSegment
-
tryAllocateOne
-
enqueueIfNeeded
-
dequeueIfDrained
-
triggerDrainIfMany
private void triggerDrainIfMany() -
serveRoundRobin
private void serveRoundRobin(int budget) -
discardEntry
-