Top Description Fields Constructors Methods
org.apache.hc.client5.http.impl.classic

public Class LinearBackoffManager

extends AbstractBackoff
Class Inheritance
Annotations
@Contract
threading:SAFE
Imports
org.apache.hc.client5.http.HttpRoute, org.apache.hc.client5.http.classic.BackoffManager, org.apache.hc.core5.annotation.Contract, .ThreadingBehavior, org.apache.hc.core5.pool.ConnPoolControl, org.apache.hc.core5.util.Args, org.slf4j.Logger, .LoggerFactory, java.time.Duration, .Instant, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.atomic.AtomicInteger

An implementation of BackoffManager that uses a linear backoff strategy to adjust the maximum number of connections per route in an org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager. This class is designed to be thread-safe and can be used in multi-threaded environments.

The linear backoff strategy increases or decreases the maximum number of connections per route by a fixed increment when backing off or probing, respectively. The adjustments are made based on a cool-down period, during which no further adjustments will be made.

The LinearBackoffManager is intended to be used with a org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager, which provides the ConnPoolControl interface. This class interacts with the PoolingHttpClientConnectionManager to adjust the maximum number of connections per route.

Example usage:

PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
LinearBackoffManager backoffManager = new LinearBackoffManager(connectionManager, 1);
// Use the backoffManager with the connectionManager in your application
Since
5.3
See Also
BackoffManager, ConnPoolControl, org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager

Field Summary

Modifier and TypeField and Description
private final int
increment

The backoff increment used when adjusting connection pool sizes.

private static final Logger
private final ConcurrentHashMap<HttpRoute, AtomicInteger>

Constructor Summary

AccessConstructor and Description
public
LinearBackoffManager(final ConnPoolControl<HttpRoute>
the connection pool control to be used by this LinearBackoffManager
connPoolControl
)

Constructs a new LinearBackoffManager with the specified connection pool control.

public
LinearBackoffManager(final ConnPoolControl<HttpRoute>
the connection pool control to be used by this LinearBackoffManager
connPoolControl
,
final int
the backoff increment to be used when adjusting connection pool sizes
increment
)

Constructs a new LinearBackoffManager with the specified connection pool control and backoff increment.

Method Summary

Modifier and TypeMethod and Description
public void
backOff(final HttpRoute
the HttpRoute for which the backoff needs to be applied
route
)

Overrides org.apache.hc.client5.http.impl.classic.AbstractBackoff.backOff.

Implements org.apache.hc.client5.http.classic.BackoffManager.backOff.

Reduces the number of maximum allowed connections for the specified route based on the exponential backoff algorithm.
protected int

Returns:

the new pool size after applying the linear backoff
getBackedOffPoolSize
(final int
the current pool size
curr
)

Implements abstract org.apache.hc.client5.http.impl.classic.AbstractBackoff.getBackedOffPoolSize.

Returns the new pool size after applying the linear backoff algorithm.
public void
probe(final HttpRoute
the HttpRoute for which the maximum number of connections will be decreased
route
)

Overrides org.apache.hc.client5.http.impl.classic.AbstractBackoff.probe.

Implements org.apache.hc.client5.http.classic.BackoffManager.probe.

Adjusts the maximum number of connections for the specified route, decreasing it by the increment value.
public void
setBackoffFactor(final double
the backoff factor, not used in the current implementation
d
)

Implements abstract org.apache.hc.client5.http.impl.classic.AbstractBackoff.setBackoffFactor.

This method is not used in LinearBackoffManager's implementation.
private boolean

Returns:

true if the cool-down period has not elapsed since the last probe or backoff, false otherwise
shouldSkip
(final HttpRoute
the HttpRoute to check
route
,
final Instant
the current Instant used to calculate the time since the last probe or backoff
now
)

Determines whether an adjustment action (backoff or probe) should be skipped for the given HttpRoute based on the cool-down period.

Inherited from org.apache.hc.client5.http.impl.classic.AbstractBackoff:
getBackoffFactorgetCapgetConnPerRoutegetCoolDowngetLastRouteBackoffsgetLastRouteProbesgetLastUpdategetTimeIntervalsetCoolDownsetPerHostConnectionCap

Field Detail

incrementback to summary
private final int increment

The backoff increment used when adjusting connection pool sizes. The pool size will be increased or decreased by this value during the backoff process. The increment must be positive.

LOGback to summary
private static final Logger LOG

Hides org.apache.hc.client5.http.impl.classic.AbstractBackoff.LOG.

routeAttemptsback to summary
private final ConcurrentHashMap<HttpRoute, AtomicInteger> routeAttempts

Constructor Detail

LinearBackoffManagerback to summary
public LinearBackoffManager(final ConnPoolControl<HttpRoute> connPoolControl)

Constructs a new LinearBackoffManager with the specified connection pool control. The backoff increment is set to 1 by default.

Parameters
connPoolControl:ConnPoolControl<HttpRoute>

the connection pool control to be used by this LinearBackoffManager

LinearBackoffManagerback to summary
public LinearBackoffManager(final ConnPoolControl<HttpRoute> connPoolControl, final int increment)

Constructs a new LinearBackoffManager with the specified connection pool control and backoff increment.

Parameters
connPoolControl:ConnPoolControl<HttpRoute>

the connection pool control to be used by this LinearBackoffManager

increment:int

the backoff increment to be used when adjusting connection pool sizes

Exceptions
IllegalArgumentException:
if connPoolControl is null or increment is not positive

Method Detail

backOffback to summary
public void backOff(final HttpRoute route)

Overrides org.apache.hc.client5.http.impl.classic.AbstractBackoff.backOff.

Implements org.apache.hc.client5.http.classic.BackoffManager.backOff.

Doc from org.apache.hc.client5.http.impl.classic.AbstractBackoff.backOff.

Reduces the number of maximum allowed connections for the specified route based on the exponential backoff algorithm.

Parameters
route:HttpRoute

the HttpRoute for which the backoff needs to be applied

Annotations
@Override
getBackedOffPoolSizeback to summary
protected int getBackedOffPoolSize(final int curr)

Implements abstract org.apache.hc.client5.http.impl.classic.AbstractBackoff.getBackedOffPoolSize.

Returns the new pool size after applying the linear backoff algorithm. The new pool size is calculated by adding the increment value to the current pool size.

Parameters
curr:int

the current pool size

Returns:int

the new pool size after applying the linear backoff

Annotations
@Override
probeback to summary
public void probe(final HttpRoute route)

Overrides org.apache.hc.client5.http.impl.classic.AbstractBackoff.probe.

Implements org.apache.hc.client5.http.classic.BackoffManager.probe.

Adjusts the maximum number of connections for the specified route, decreasing it by the increment value. The method ensures that adjustments only happen after the cool-down period has passed since the last adjustment.

Parameters
route:HttpRoute

the HttpRoute for which the maximum number of connections will be decreased

Annotations
@Override
setBackoffFactorback to summary
public void setBackoffFactor(final double d)

Implements abstract org.apache.hc.client5.http.impl.classic.AbstractBackoff.setBackoffFactor.

This method is not used in LinearBackoffManager's implementation. It is provided to fulfill the interface requirement and for potential future extensions or modifications of LinearBackoffManager that may use the backoff factor.

Parameters
d:double

the backoff factor, not used in the current implementation

Annotations
@Override
shouldSkipback to summary
private boolean shouldSkip(final HttpRoute route, final Instant now)

Determines whether an adjustment action (backoff or probe) should be skipped for the given HttpRoute based on the cool-down period. If the time elapsed since the last successful probe or backoff for the given route is less than the cool-down period, the method returns true. Otherwise, it returns false.

This method is used by both backOff() and probe() methods to enforce the cool-down period before making adjustments to the connection pool size.

Parameters
route:HttpRoute

the HttpRoute to check

now:Instant

the current Instant used to calculate the time since the last probe or backoff

Returns:boolean

true if the cool-down period has not elapsed since the last probe or backoff, false otherwise