Class JitterDelayRetryPolicy
- All Implemented Interfaces:
RetryPolicy
This policy implements the RetryPolicy interface and provides exponential backoff with
jitter for retrying failed HTTP requests.
The delay for each retry attempt is calculated as: initialDelayMs * multiplier ^
(attempt - 1) * (1 ± jitterFactor)
Jitter helps prevent the "thundering herd" problem where many clients retry simultaneously.
The jitter factor randomizes the delay within the range [baseDelay * (1 - jitterFactor),
baseDelay * (1 + jitterFactor)].
Default configuration:
DEFAULT_MAX_ATTEMPTS: 3 - maximum attempts per request (includes initial try).DEFAULT_INITIAL_DELAY_MS: 500ms - initial backoff delay.DEFAULT_DELAY_MULTIPLIER: 2.0 - exponential multiplier (e.g., 2.0 doubles the wait each retry).DEFAULT_DELAY_JITTER_FACTOR: 0.5 - jitter fraction in [0, 1) applied to the computed delay (e.g., 0.5 randomizes by ±50%).
Example usage:
RetryPolicy policy = JitterDelayRetryPolicy.builder()
.maxAttempts(5)
.initDelayMs(100L)
.delayMultiplier(2.0)
.delayJitterFactor(0.2)
.build();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for creatingJitterDelayRetryPolicyinstances with custom configuration. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final doublestatic final doublestatic final longstatic final int -
Method Summary
Modifier and TypeMethodDescriptionbooleanallowRetry(int attempt) Determines whether a retry should be attempted based on the current attempt number.builder()Creates a new builder for constructing aJitterDelayRetryPolicy.intReturns the maximum number of retry attempts allowed by this policy.sleepTime(int attempt) Calculates the sleep time before the next retry attempt using exponential backoff with jitter.
-
Field Details
-
DEFAULT_MAX_ATTEMPTS
public static final int DEFAULT_MAX_ATTEMPTS- See Also:
-
DEFAULT_INITIAL_DELAY_MS
public static final long DEFAULT_INITIAL_DELAY_MS- See Also:
-
DEFAULT_DELAY_MULTIPLIER
public static final double DEFAULT_DELAY_MULTIPLIER- See Also:
-
DEFAULT_DELAY_JITTER_FACTOR
public static final double DEFAULT_DELAY_JITTER_FACTOR- See Also:
-
-
Method Details
-
maxAttempts
public int maxAttempts()Description copied from interface:RetryPolicyReturns the maximum number of retry attempts allowed by this policy.This value determines the upper bound on retry attempts. A return value of 0 means no retries will be attempted (only the initial attempt), while a positive value indicates the maximum number of additional attempts after the initial failure.
- Specified by:
maxAttemptsin interfaceRetryPolicy- Returns:
- the maximum number of retry attempts, must be non-negative
-
sleepTime
Calculates the sleep time before the next retry attempt using exponential backoff with jitter.- Specified by:
sleepTimein interfaceRetryPolicy- Parameters:
attempt- the current attempt number (1-indexed)- Returns:
- the duration to sleep before retrying
-
allowRetry
public boolean allowRetry(int attempt) Determines whether a retry should be attempted based on the current attempt number.- Specified by:
allowRetryin interfaceRetryPolicy- Parameters:
attempt- the current attempt number (1-indexed)- Returns:
- true if retry is allowed, false otherwise
-
builder
Creates a new builder for constructing aJitterDelayRetryPolicy.- Returns:
- a new builder instance with default values
-