Package org.apache.hc.core5.pool
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:
java.io.Closeable,java.lang.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 java.lang.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:
ManagedConnPool,PoolReusePolicy,DisposalCallback
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) classRouteSegmentedConnPool.Segment(package private) classRouteSegmentedConnPool.Waiter
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicBooleanclosedprivate java.util.concurrent.atomic.AtomicIntegerdefaultMaxPerRouteprivate DisposalCallback<C>disposalprivate java.util.concurrent.ThreadPoolExecutordisposerDedicated executor for asynchronous, best-effort disposal.private java.util.concurrent.atomic.AtomicBooleandrainingprivate java.util.concurrent.ConcurrentHashMap<R,java.lang.Integer>maxPerRouteprivate java.util.concurrent.atomic.AtomicIntegermaxTotalprivate java.util.concurrent.ConcurrentLinkedQueue<R>pendingQueueprivate java.util.concurrent.atomic.AtomicIntegerpendingRouteCountprivate PoolReusePolicyreusePolicyprivate static intRR_BUDGETprivate static intRR_MIN_PENDING_ROUTESprivate java.util.concurrent.ConcurrentHashMap<R,RouteSegmentedConnPool.Segment>segmentsprivate java.util.concurrent.ScheduledExecutorServicetimeoutsprivate TimeValuetimeToLiveprivate java.util.concurrent.atomic.AtomicIntegertotalAllocated
-
Constructor Summary
Constructors Constructor Description RouteSegmentedConnPool(int defaultMaxPerRoute, int maxTotal, TimeValue timeToLive, PoolReusePolicy reusePolicy, DisposalCallback<C> disposal)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcancelTimeout(RouteSegmentedConnPool.Waiter w)voidclose()voidclose(CloseMode closeMode)Closes this process or endpoint and releases any system resources associated with it.voidcloseExpired()voidcloseIdle(TimeValue idleTime)private booleancompatible(java.lang.Object needed, java.lang.Object have)private voiddequeueIfDrained(RouteSegmentedConnPool.Segment seg)private 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.Segment seg)private voidensureOpen()intgetDefaultMaxPerRoute()intgetMaxPerRoute(R route)intgetMaxTotal()java.util.Set<R>getRoutes()PoolStatsgetStats(R route)PoolStatsgetTotalStats()private booleanhandOffToCompatibleWaiter(PoolEntry<R,C> entry, RouteSegmentedConnPool.Segment seg)private booleanisPastTtl(PoolEntry<R,C> p)java.util.concurrent.Future<PoolEntry<R,C>>lease(R route, java.lang.Object state, Timeout requestTimeout, FutureCallback<PoolEntry<R,C>> callback)Attempts to lease a connection for the given route and with the given state from the pool.private voidmaybeCleanupSegment(R route, RouteSegmentedConnPool.Segment seg)private voidofferAvailable(RouteSegmentedConnPool.Segment seg, PoolEntry<R,C> p)private CloseModeorImmediate(CloseMode m)private PoolEntry<R,C>pollAvailable(RouteSegmentedConnPool.Segment seg, java.lang.Object neededState)voidrelease(PoolEntry<R,C> entry, boolean reusable)Releases the pool entry back to the pool.private voidscheduleTimeout(RouteSegmentedConnPool.Waiter w, RouteSegmentedConnPool.Segment seg)private voidserveRoundRobin(int budget)voidsetDefaultMaxPerRoute(int max)voidsetMaxPerRoute(R route, int max)voidsetMaxTotal(int max)private voidtriggerDrainIfMany()private booleantryAllocateOne(R route, RouteSegmentedConnPool.Segment seg)
-
-
-
Field Detail
-
RR_MIN_PENDING_ROUTES
private static final int RR_MIN_PENDING_ROUTES
- See Also:
- Constant Field Values
-
RR_BUDGET
private static final int RR_BUDGET
- See Also:
- Constant Field Values
-
reusePolicy
private final PoolReusePolicy reusePolicy
-
timeToLive
private final TimeValue timeToLive
-
disposal
private final DisposalCallback<C extends ModalCloseable> disposal
-
defaultMaxPerRoute
private final java.util.concurrent.atomic.AtomicInteger defaultMaxPerRoute
-
segments
private final java.util.concurrent.ConcurrentHashMap<R,RouteSegmentedConnPool.Segment> segments
-
maxPerRoute
private final java.util.concurrent.ConcurrentHashMap<R,java.lang.Integer> maxPerRoute
-
totalAllocated
private final java.util.concurrent.atomic.AtomicInteger totalAllocated
-
maxTotal
private final java.util.concurrent.atomic.AtomicInteger maxTotal
-
closed
private final java.util.concurrent.atomic.AtomicBoolean closed
-
timeouts
private final java.util.concurrent.ScheduledExecutorService timeouts
-
disposer
private final java.util.concurrent.ThreadPoolExecutor disposer
Dedicated executor for asynchronous, best-effort disposal. Bounded queue; on saturation we fall back to IMMEDIATE close on the caller thread.
-
pendingQueue
private final java.util.concurrent.ConcurrentLinkedQueue<R> pendingQueue
-
draining
private final java.util.concurrent.atomic.AtomicBoolean draining
-
pendingRouteCount
private final java.util.concurrent.atomic.AtomicInteger pendingRouteCount
-
-
Constructor Detail
-
RouteSegmentedConnPool
public RouteSegmentedConnPool(int defaultMaxPerRoute, int maxTotal, TimeValue timeToLive, PoolReusePolicy reusePolicy, DisposalCallback<C> disposal)
-
-
Method Detail
-
lease
public java.util.concurrent.Future<PoolEntry<R,C>> lease(R route, java.lang.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
public void release(PoolEntry<R,C> entry, boolean reusable)
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
public void close() throws java.io.IOException- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
java.io.IOException
-
close
public void close(CloseMode closeMode)
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
public void closeIdle(TimeValue idleTime)
- Specified by:
closeIdlein interfaceConnPoolControl<R>
-
closeExpired
public void closeExpired()
- Specified by:
closeExpiredin interfaceConnPoolControl<R>
-
getRoutes
public java.util.Set<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
public int getMaxPerRoute(R route)
- Specified by:
getMaxPerRoutein interfaceConnPoolControl<R>
-
setMaxPerRoute
public void setMaxPerRoute(R route, int max)
- Specified by:
setMaxPerRoutein interfaceConnPoolControl<R>
-
getTotalStats
public PoolStats getTotalStats()
- Specified by:
getTotalStatsin interfaceConnPoolStats<R>
-
getStats
public PoolStats getStats(R route)
- Specified by:
getStatsin interfaceConnPoolStats<R>
-
ensureOpen
private void ensureOpen()
-
scheduleTimeout
private void scheduleTimeout(RouteSegmentedConnPool.Waiter w, RouteSegmentedConnPool.Segment seg)
-
cancelTimeout
private void cancelTimeout(RouteSegmentedConnPool.Waiter w)
-
offerAvailable
private void offerAvailable(RouteSegmentedConnPool.Segment seg, PoolEntry<R,C> p)
-
pollAvailable
private PoolEntry<R,C> pollAvailable(RouteSegmentedConnPool.Segment seg, java.lang.Object neededState)
-
compatible
private boolean compatible(java.lang.Object needed, java.lang.Object have)
-
handOffToCompatibleWaiter
private boolean handOffToCompatibleWaiter(PoolEntry<R,C> entry, RouteSegmentedConnPool.Segment seg)
-
maybeCleanupSegment
private void maybeCleanupSegment(R route, RouteSegmentedConnPool.Segment seg)
-
tryAllocateOne
private boolean tryAllocateOne(R route, RouteSegmentedConnPool.Segment seg)
-
enqueueIfNeeded
private void enqueueIfNeeded(R route, RouteSegmentedConnPool.Segment seg)
-
dequeueIfDrained
private void dequeueIfDrained(RouteSegmentedConnPool.Segment seg)
-
triggerDrainIfMany
private void triggerDrainIfMany()
-
serveRoundRobin
private void serveRoundRobin(int budget)
-
-