Class RetryPolicy
java.lang.Object
express.mvp.myra.transport.error.RetryPolicy
Defines retry behavior for failed operations.
This class encapsulates retry policy decisions including whether to retry, delay calculation, and maximum attempt limits. It supports exponential backoff with jitter for distributed systems.
Built-in Policies
noRetry()- Never retryimmediate(int)- Retry immediately without delayfixedDelay(int, Duration)- Fixed delay between retriesexponentialBackoff(int, Duration, Duration)- Exponential backoff with capexponentialBackoffWithJitter(int, Duration, Duration, double)- With jitter
Usage Example
RetryPolicy policy = RetryPolicy.exponentialBackoffWithJitter(
5, // max 5 attempts
Duration.ofMillis(100), // start with 100ms
Duration.ofSeconds(30), // cap at 30s
0.2 // 20% jitter
);
RetryContext context = new RetryContext("connect", policy.getMaxAttempts());
while (true) {
try {
context.startAttempt();
connect();
break;
} catch (Exception e) {
context.recordFailure(e);
if (policy.shouldRetry(context)) {
long delay = policy.calculateDelay(context);
Thread.sleep(delay);
context.recordDelay(delay);
} else {
throw new ConnectionException("Failed after retries", e);
}
}
}
- See Also:
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic RetryPolicy.Builderbuilder()Returns a builder for custom policy configuration.longcalculateDelay(RetryContext context) Calculates the delay before the next retry.static RetryPolicyexponentialBackoff(int maxAttempts, Duration initialDelay, Duration maxDelay) Returns a policy with exponential backoff.static RetryPolicyexponentialBackoffWithJitter(int maxAttempts, Duration initialDelay, Duration maxDelay, double jitterFactor) Returns a policy with exponential backoff and jitter.static RetryPolicyfixedDelay(int maxAttempts, Duration delay) Returns a policy with fixed delay between retries.intReturns the maximum number of attempts.static RetryPolicyimmediate(int maxAttempts) Returns a policy that retries immediately without delay.static RetryPolicynoRetry()Returns a policy that never retries.booleanshouldRetry(RetryContext context) Determines if a retry should be attempted.
-
Method Details
-
getMaxAttempts
public int getMaxAttempts()Returns the maximum number of attempts.- Returns:
- max attempts
-
shouldRetry
Determines if a retry should be attempted.- Parameters:
context- the retry context- Returns:
- true if retry should be attempted
-
calculateDelay
Calculates the delay before the next retry.- Parameters:
context- the retry context- Returns:
- delay in milliseconds
-
noRetry
-
immediate
Returns a policy that retries immediately without delay.- Parameters:
maxAttempts- maximum attempts- Returns:
- immediate retry policy
-
fixedDelay
Returns a policy with fixed delay between retries.- Parameters:
maxAttempts- maximum attemptsdelay- delay between retries- Returns:
- fixed delay policy
-
exponentialBackoff
public static RetryPolicy exponentialBackoff(int maxAttempts, Duration initialDelay, Duration maxDelay) Returns a policy with exponential backoff.- Parameters:
maxAttempts- maximum attemptsinitialDelay- initial delaymaxDelay- maximum delay cap- Returns:
- exponential backoff policy
-
exponentialBackoffWithJitter
public static RetryPolicy exponentialBackoffWithJitter(int maxAttempts, Duration initialDelay, Duration maxDelay, double jitterFactor) Returns a policy with exponential backoff and jitter.Jitter helps prevent thundering herd problems when many clients retry simultaneously.
- Parameters:
maxAttempts- maximum attemptsinitialDelay- initial delaymaxDelay- maximum delay capjitterFactor- jitter factor (0.0-1.0, e.g., 0.2 for ±20%)- Returns:
- exponential backoff with jitter policy
-
builder
Returns a builder for custom policy configuration.- Returns:
- new builder
-