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 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 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.
    private final Map<HttpRoute,Instant>
    A map that stores the last backoff timestamp for each HTTP route.
    private final Map<HttpRoute,Instant>
    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

    Constructors
    Constructor
    Description
    AbstractBackoff(org.apache.hc.core5.pool.ConnPoolControl<HttpRoute> connPerRoute)
    Constructs a new ExponentialBackoffManager with the specified connection pool control.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Reduces the number of maximum allowed connections for the specified route based on the exponential backoff algorithm.
    protected abstract int
    Calculates the new pool size after applying the exponential backoff algorithm.
    Returns the backoff factor as an AtomicReference of Double.
    protected AtomicInteger
    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.
    protected Map<HttpRoute,Instant>
    Returns the map containing the last backoff times for each HttpRoute.
    protected Map<HttpRoute,Instant>
    Returns the map containing the last probe times for each HttpRoute.
    long
    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
    Sets the per-host connection cap.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • 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 Map<HttpRoute,Instant> lastRouteProbes
      A map that stores the last probe timestamp for each HTTP route.
    • lastRouteBackoffs

      private final Map<HttpRoute,Instant> lastRouteBackoffs
      A map that stores the last backoff timestamp for each HTTP route.
    • coolDown

      private final 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 AtomicReference<Double> backoffFactor
      The growth rate used in the exponential backoff algorithm.
    • cap

      private final AtomicInteger cap
      The per-host connection cap, as defined in RFC 2616 sec 8.1.4.
    • timeInterval

      private final AtomicInteger timeInterval
      The number of time intervals used in the exponential backoff algorithm.
  • Constructor Details

    • 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:
      IllegalArgumentException - if connPerRoute is null
  • Method Details

    • 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 interface BackoffManager
      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 interface BackoffManager
      Parameters:
      route - the HttpRoute for which the probe needs to be applied
    • getLastUpdate

      public long getLastUpdate(Map<HttpRoute,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 HttpRoutes
      route - 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

      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 Map<HttpRoute,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 Map<HttpRoute,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 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 AtomicReference<Double> getBackoffFactor()
      Returns the backoff factor as an AtomicReference of Double.
      Returns:
      the AtomicReference containing the backoff factor
    • getCap

      protected 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 AtomicInteger getTimeInterval()
      Returns the time interval between backoff and probe operations as an AtomicInteger.
      Returns:
      the AtomicInteger containing the time interval