Package com.tnsai.resilience
Class RetryPolicy
java.lang.Object
com.tnsai.resilience.RetryPolicy
Immutable retry policy definition used by the resilience layer.
Supports exponential backoff, jitter and fine-grained retry conditions.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic RetryPolicy.Builderbuilder()static RetryPolicystatic RetryPolicyfrom(Resilience.Retry retry) Builds aRetryPolicyfrom aResilience.Retryannotation, mapping its millisecond fields toDurations and itsjitterflag to a jitter factor.doubleintdoubleCalculates the next backoff duration using exponential growth with jitter.booleanshouldRetry(Exception exception, int attempt) Returns true if the operation should be retried for the given exception and attempt number.
-
Method Details
-
getMaxAttempts
public int getMaxAttempts() -
getInitialBackoff
-
getMaxBackoff
-
getMultiplier
public double getMultiplier() -
getJitterFactor
public double getJitterFactor() -
getExtraCondition
-
shouldRetry
Returns true if the operation should be retried for the given exception and attempt number.- Parameters:
exception- The raised exceptionattempt- The current attempt count (1 = first retry)
-
nextDelay
Calculates the next backoff duration using exponential growth with jitter. Handles potential overflow by capping at maxBackoff.- Parameters:
currentDelay- Current delay- Returns:
- Next delay duration
-
builder
-
defaultPolicy
-
from
Builds aRetryPolicyfrom aResilience.Retryannotation, mapping its millisecond fields toDurations and itsjitterflag to a jitter factor. Only exception-typed entries inretryOn/noRetryOnare honoured (the annotation permitsThrowable, but the policy operates onException).The annotation's
maxAttemptsdefault of0means "no retry"; callers should consultretry.maxAttempts() > 0before wiring a policy. This factory floorsmaxAttemptsat 1 so the resulting policy is always constructible.- Parameters:
retry- the annotation's retry configuration- Returns:
- an equivalent immutable retry policy
-