Class AbstractBackoff
java.lang.Object
org.apache.hc.client5.http.impl.classic.AbstractBackoff
- All Implemented Interfaces:
BackoffManager
- Direct Known Subclasses:
AIMDBackoffManager
,ExponentialBackoffManager
,LinearBackoffManager
AbstractBackoff is an abstract class that provides a common implementation for managing
backoff behavior in HttpClient connection pool. Subclasses should implement the specific
backoff algorithms by overriding the abstract methods.
This class provides common functionality for maintaining the route-wise backoff and probe timestamps, as well as the cool-down period for each backoff attempt.
It also contains the basic structure of the backOff and probe methods, which use the route-wise timestamps to determine if the backoff or probe should be applied, and then call the specific algorithm implementation for calculating the new pool size.
- Since:
- 5.3
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final AtomicReference
<Double> The growth rate used in the exponential backoff algorithm.private final AtomicInteger
The per-host connection cap, as defined in RFC 2616 sec 8.1.4.private final org.apache.hc.core5.pool.ConnPoolControl
<HttpRoute> Connection pool control responsible for managing the maximum number of connections per HTTP route.private final AtomicReference
<org.apache.hc.core5.util.TimeValue> The cool-down period after which the backoff or probe process can be performed again.A map that stores the last backoff timestamp for each HTTP route.A map that stores the last probe timestamp for each HTTP route.private static final org.slf4j.Logger
private final AtomicInteger
The number of time intervals used in the exponential backoff algorithm. -
Constructor Summary
ConstructorsConstructorDescriptionAbstractBackoff
(org.apache.hc.core5.pool.ConnPoolControl<HttpRoute> connPerRoute) Constructs a new ExponentialBackoffManager with the specified connection pool control. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Reduces the number of maximum allowed connections for the specified route based on the exponential backoff algorithm.protected abstract int
getBackedOffPoolSize
(int curr) Calculates the new pool size after applying the exponential backoff algorithm.protected AtomicReference
<Double> Returns the backoff factor as an AtomicReference of Double.protected AtomicInteger
getCap()
Returns the cap on the maximum number of connections per route as an AtomicInteger.protected org.apache.hc.core5.pool.ConnPoolControl
<HttpRoute> Returns the connection pool control for managing the maximum number of connections per route.protected AtomicReference
<org.apache.hc.core5.util.TimeValue> Returns the cool down period between backoff and probe operations as an AtomicReference of TimeValue.Returns the map containing the last backoff times for each HttpRoute.Returns the map containing the last probe times for each HttpRoute.long
getLastUpdate
(Map<HttpRoute, Long> updates, HttpRoute route) Retrieves the last update timestamp for the specified route from the provided updates map.protected AtomicInteger
Returns the time interval between backoff and probe operations as an AtomicInteger.void
Increases the number of maximum allowed connections for the specified route after a successful connection has been established.(package private) abstract void
setBackoffFactor
(double d) Sets the backoff factor for the backoff algorithm.void
setCoolDown
(org.apache.hc.core5.util.TimeValue coolDown) Sets the cool-down time value for adjustments in pool sizes for a given host.void
setPerHostConnectionCap
(int cap) Sets the per-host connection cap.
-
Field Details
-
LOG
private static final org.slf4j.Logger LOG -
connPerRoute
Connection pool control responsible for managing the maximum number of connections per HTTP route. -
lastRouteProbes
A map that stores the last probe timestamp for each HTTP route. -
lastRouteBackoffs
A map that stores the last backoff timestamp for each HTTP route. -
coolDown
The cool-down period after which the backoff or probe process can be performed again. -
backoffFactor
The growth rate used in the exponential backoff algorithm. -
cap
The per-host connection cap, as defined in RFC 2616 sec 8.1.4. -
timeInterval
The number of time intervals used in the exponential backoff algorithm.
-
-
Constructor Details
-
AbstractBackoff
Constructs a new ExponentialBackoffManager with the specified connection pool control.- Parameters:
connPerRoute
- the connection pool control to be used for managing connections- Throws:
IllegalArgumentException
- if connPerRoute is null
-
-
Method Details
-
backOff
Reduces the number of maximum allowed connections for the specified route based on the exponential backoff algorithm.- Specified by:
backOff
in interfaceBackoffManager
- Parameters:
route
- the HttpRoute for which the backoff needs to be applied
-
getBackedOffPoolSize
protected abstract int getBackedOffPoolSize(int curr) Calculates the new pool size after applying the exponential backoff algorithm. The new pool size is calculated using the formula: floor(curr / (1 + growthRate) ^ t), where curr is the current pool size, growthRate is the exponential growth rate, and t is the time interval.- Parameters:
curr
- the current pool size- Returns:
- the new pool size after applying the backoff
-
probe
Increases the number of maximum allowed connections for the specified route after a successful connection has been established.- Specified by:
probe
in interfaceBackoffManager
- Parameters:
route
- the HttpRoute for which the probe needs to be applied
-
getLastUpdate
Retrieves the last update timestamp for the specified route from the provided updates map.- Parameters:
updates
- the map containing update timestamps for HttpRoutesroute
- the HttpRoute for which the last update timestamp is needed- Returns:
- the last update timestamp for the specified route or 0L if not present in the map
-
setPerHostConnectionCap
public void setPerHostConnectionCap(int cap) Sets the per-host connection cap.- Parameters:
cap
- the per-host connection cap to be set- Throws:
IllegalArgumentException
- if the cap is not positive
-
setBackoffFactor
abstract void setBackoffFactor(double d) Sets the backoff factor for the backoff algorithm. The backoff factor should be a value between 0.0 and 1.0. The specific implementation of how the backoff factor is used should be provided by subclasses.- Parameters:
d
- the backoff factor to be set
-
setCoolDown
public void setCoolDown(org.apache.hc.core5.util.TimeValue coolDown) Sets the cool-down time value for adjustments in pool sizes for a given host. This time value allows enough time for the adjustments to take effect before further adjustments are made. The cool-down time value must be positive and not null.- Parameters:
coolDown
- the TimeValue representing the cool-down period between adjustments- Throws:
IllegalArgumentException
- if the provided cool-down time value is null or non-positive
-
getConnPerRoute
Returns the connection pool control for managing the maximum number of connections per route.- Returns:
- the connection pool control instance
-
getLastRouteProbes
Returns the map containing the last probe times for each HttpRoute.- Returns:
- the map of HttpRoute to Instant representing the last probe times
-
getLastRouteBackoffs
Returns the map containing the last backoff times for each HttpRoute.- Returns:
- the map of HttpRoute to Instant representing the last backoff times
-
getCoolDown
Returns the cool down period between backoff and probe operations as an AtomicReference of TimeValue.- Returns:
- the AtomicReference containing the cool down period
-
getBackoffFactor
Returns the backoff factor as an AtomicReference of Double.- Returns:
- the AtomicReference containing the backoff factor
-
getCap
Returns the cap on the maximum number of connections per route as an AtomicInteger.- Returns:
- the AtomicInteger containing the cap value
-
getTimeInterval
Returns the time interval between backoff and probe operations as an AtomicInteger.- Returns:
- the AtomicInteger containing the time interval
-