Class AbstractBackoff
- java.lang.Object
-
- org.apache.hc.client5.http.impl.classic.AbstractBackoff
-
- All Implemented Interfaces:
BackoffManager
- Direct Known Subclasses:
AIMDBackoffManager
,ExponentialBackoffManager
,LinearBackoffManager
public abstract class AbstractBackoff extends java.lang.Object implements BackoffManager
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
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicReference<java.lang.Double>
backoffFactor
The growth rate used in the exponential backoff algorithm.private java.util.concurrent.atomic.AtomicInteger
cap
The per-host connection cap, as defined in RFC 2616 sec 8.1.4.private org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
connPerRoute
Connection pool control responsible for managing the maximum number of connections per HTTP route.private java.util.concurrent.atomic.AtomicReference<org.apache.hc.core5.util.TimeValue>
coolDown
The cool-down period after which the backoff or probe process can be performed again.private java.util.Map<HttpRoute,java.time.Instant>
lastRouteBackoffs
A map that stores the last backoff timestamp for each HTTP route.private java.util.Map<HttpRoute,java.time.Instant>
lastRouteProbes
A map that stores the last probe timestamp for each HTTP route.private static org.slf4j.Logger
LOG
private java.util.concurrent.atomic.AtomicInteger
timeInterval
The number of time intervals used in the exponential backoff algorithm.
-
Constructor Summary
Constructors Constructor Description AbstractBackoff(org.apache.hc.core5.pool.ConnPoolControl<HttpRoute> connPerRoute)
Constructs a new ExponentialBackoffManager with the specified connection pool control.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
backOff(HttpRoute route)
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 java.util.concurrent.atomic.AtomicReference<java.lang.Double>
getBackoffFactor()
Returns the backoff factor as an AtomicReference of Double.protected java.util.concurrent.atomic.AtomicInteger
getCap()
Returns the cap on the maximum number of connections per route as an AtomicInteger.protected org.apache.hc.core5.pool.ConnPoolControl<HttpRoute>
getConnPerRoute()
Returns the connection pool control for managing the maximum number of connections per route.protected java.util.concurrent.atomic.AtomicReference<org.apache.hc.core5.util.TimeValue>
getCoolDown()
Returns the cool down period between backoff and probe operations as an AtomicReference of TimeValue.protected java.util.Map<HttpRoute,java.time.Instant>
getLastRouteBackoffs()
Returns the map containing the last backoff times for each HttpRoute.protected java.util.Map<HttpRoute,java.time.Instant>
getLastRouteProbes()
Returns the map containing the last probe times for each HttpRoute.long
getLastUpdate(java.util.Map<HttpRoute,java.lang.Long> updates, HttpRoute route)
Retrieves the last update timestamp for the specified route from the provided updates map.protected java.util.concurrent.atomic.AtomicInteger
getTimeInterval()
Returns the time interval between backoff and probe operations as an AtomicInteger.void
probe(HttpRoute route)
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 Detail
-
LOG
private static final org.slf4j.Logger LOG
-
connPerRoute
private final org.apache.hc.core5.pool.ConnPoolControl<HttpRoute> connPerRoute
Connection pool control responsible for managing the maximum number of connections per HTTP route.
-
lastRouteProbes
private final java.util.Map<HttpRoute,java.time.Instant> lastRouteProbes
A map that stores the last probe timestamp for each HTTP route.
-
lastRouteBackoffs
private final java.util.Map<HttpRoute,java.time.Instant> lastRouteBackoffs
A map that stores the last backoff timestamp for each HTTP route.
-
coolDown
private final java.util.concurrent.atomic.AtomicReference<org.apache.hc.core5.util.TimeValue> coolDown
The cool-down period after which the backoff or probe process can be performed again.
-
backoffFactor
private final java.util.concurrent.atomic.AtomicReference<java.lang.Double> backoffFactor
The growth rate used in the exponential backoff algorithm.
-
cap
private final java.util.concurrent.atomic.AtomicInteger cap
The per-host connection cap, as defined in RFC 2616 sec 8.1.4.
-
timeInterval
private final java.util.concurrent.atomic.AtomicInteger timeInterval
The number of time intervals used in the exponential backoff algorithm.
-
-
Constructor Detail
-
AbstractBackoff
public AbstractBackoff(org.apache.hc.core5.pool.ConnPoolControl<HttpRoute> connPerRoute)
Constructs a new ExponentialBackoffManager with the specified connection pool control.- Parameters:
connPerRoute
- the connection pool control to be used for managing connections- Throws:
java.lang.IllegalArgumentException
- if connPerRoute is null
-
-
Method Detail
-
backOff
public void backOff(HttpRoute route)
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
public void probe(HttpRoute route)
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
public long getLastUpdate(java.util.Map<HttpRoute,java.lang.Long> updates, HttpRoute route)
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:
java.lang.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:
java.lang.IllegalArgumentException
- if the provided cool-down time value is null or non-positive
-
getConnPerRoute
protected org.apache.hc.core5.pool.ConnPoolControl<HttpRoute> getConnPerRoute()
Returns the connection pool control for managing the maximum number of connections per route.- Returns:
- the connection pool control instance
-
getLastRouteProbes
protected java.util.Map<HttpRoute,java.time.Instant> 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
protected java.util.Map<HttpRoute,java.time.Instant> 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
protected java.util.concurrent.atomic.AtomicReference<org.apache.hc.core5.util.TimeValue> 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
protected java.util.concurrent.atomic.AtomicReference<java.lang.Double> getBackoffFactor()
Returns the backoff factor as an AtomicReference of Double.- Returns:
- the AtomicReference containing the backoff factor
-
getCap
protected java.util.concurrent.atomic.AtomicInteger getCap()
Returns the cap on the maximum number of connections per route as an AtomicInteger.- Returns:
- the AtomicInteger containing the cap value
-
getTimeInterval
protected java.util.concurrent.atomic.AtomicInteger getTimeInterval()
Returns the time interval between backoff and probe operations as an AtomicInteger.- Returns:
- the AtomicInteger containing the time interval
-
-