Compartilhar via


ExponentialBackoff Class

  • java.lang.Object
    • com.azure.core.http.policy.ExponentialBackoff

Implements

public class ExponentialBackoff
implements RetryStrategy

The ExponentialBackoff class is an implementation of the RetryStrategy interface. This strategy uses a delay duration that exponentially increases with each retry attempt until an upper bound is reached, after which every retry attempt is delayed by the provided max delay duration.

This class is useful when you need to handle retries for operations that may transiently fail. It ensures that the retries are performed with an increasing delay to avoid overloading the system.

Code sample:

In this example, an ExponentialBackoff is created and used in a RetryPolicy which can be added to a pipeline. For a request sent by the pipeline, if the server responds with a transient error, the request will be retried with an exponentially increasing delay.

ExponentialBackoff retryStrategy = new ExponentialBackoff();
 RetryPolicy policy = new RetryPolicy(retryStrategy);

Constructor Summary

Constructor Description
ExponentialBackoff()

Creates an instance of ExponentialBackoff with a maximum number of retry attempts configured by the environment property PROPERTY_AZURE_REQUEST_RETRY_COUNT, or three if it isn't configured or is less than or equal to 0.

ExponentialBackoff(ExponentialBackoffOptions options)

Creates an instance of ExponentialBackoff.

ExponentialBackoff(int maxRetries, Duration baseDelay, Duration maxDelay)

Creates an instance of ExponentialBackoff.

Method Summary

Modifier and Type Method and Description
Duration calculateRetryDelay(int retryAttempts)

Computes the delay between each retry.

int getMaxRetries()

Max number of retry attempts to be make.

boolean shouldRetryCondition(RequestRetryCondition requestRetryCondition)

This method is consulted to determine if a retry attempt should be made for the given RequestRetryCondition.

Methods inherited from java.lang.Object

Constructor Details

ExponentialBackoff

public ExponentialBackoff()

Creates an instance of ExponentialBackoff with a maximum number of retry attempts configured by the environment property PROPERTY_AZURE_REQUEST_RETRY_COUNT, or three if it isn't configured or is less than or equal to 0. This strategy starts with a delay of 800 milliseconds and exponentially increases with each additional retry attempt to a maximum of 8 seconds.

ExponentialBackoff

public ExponentialBackoff(ExponentialBackoffOptions options)

Creates an instance of ExponentialBackoff.

Parameters:

options - The ExponentialBackoffOptions.

ExponentialBackoff

public ExponentialBackoff(int maxRetries, Duration baseDelay, Duration maxDelay)

Creates an instance of ExponentialBackoff.

Parameters:

maxRetries - The max retry attempts that can be made.
baseDelay - The base delay duration for retry.
maxDelay - The max delay duration for retry.

Method Details

calculateRetryDelay

public Duration calculateRetryDelay(int retryAttempts)

Computes the delay between each retry.

Parameters:

retryAttempts

getMaxRetries

public int getMaxRetries()

Max number of retry attempts to be make.

shouldRetryCondition

public boolean shouldRetryCondition(RequestRetryCondition requestRetryCondition)

This method is consulted to determine if a retry attempt should be made for the given RequestRetryCondition.

By default, if the RequestRetryCondition contains a non-null HttpResponse, then the #shouldRetry(HttpResponse) method is called, otherwise the #shouldRetryException(Throwable) method is called.

Parameters:

requestRetryCondition

Applies to