Class RetryPolicy
IOExceptions,
using exponential backoff with full jitter and honoring a Retry-After header when present.
To avoid duplicate side effects, retries of 5xx responses and IOExceptions are by
default applied only to idempotent methods - everything except POST and
PATCH (RFC 5789: PATCH is not idempotent in general, and the SDK sends RFC 6902
documents whose add operations would apply twice). A transient failure that occurs after
the server has already processed a POST create or a PATCH would otherwise re-send
it and duplicate the side effect. Set retryNonIdempotentMethods if your endpoints are
safe to retry (e.g. they dedupe via an idempotency key, or the POST is a side-effect-free
search).
A 429 response is exempt from the idempotency gate: it means the server explicitly did
not process the request, so re-sending is always safe - 429s are retried for every
method (honoring Retry-After) as long as 429 is in the retryable status set.
Retries are bounded by getMaxRetries() attempts beyond the initial request. Pass
none() to disable retries entirely, or build a custom policy via the constructor.
- Author:
- ianjones
-
Constructor Summary
ConstructorsConstructorDescriptionRetryPolicy(int maxRetries, long baseDelayMillis, long maxDelayMillis, Set<Integer> retryableStatusCodes, boolean retryOnIoException, boolean honorRetryAfter) Constructor for RetryPolicy that retries only idempotent methods (POST and PATCH are never retried).RetryPolicy(int maxRetries, long baseDelayMillis, long maxDelayMillis, Set<Integer> retryableStatusCodes, boolean retryOnIoException, boolean honorRetryAfter, boolean retryNonIdempotentMethods) Full constructor. -
Method Summary
Modifier and TypeMethodDescriptionlongcomputeBackoffMillis(int attempt, Long retryAfterMillis) Computes the backoff wait before the given retry attempt.static RetryPolicyThe default policy: up to 3 retries on 429/500/502/503/504 and transient IO errors, 500ms base backoff capped at 20s, withRetry-Afterhonored.intbooleanisRetryable(int statusCode, HttpMethod method) Whether a response with the given status code should be retried for a request with the given method.booleanisRetryableMethod(HttpMethod method) Whether a request with the given HTTP method is eligible for retry of a failure that may have had server-side effects (a 5xx response or a transportIOException).booleanisRetryableStatus(int statusCode) Whether a response with the given status code should be retried.booleanWhether transient IO exceptions are retried.static RetryPolicynone()A policy that performs no retries.
-
Constructor Details
-
RetryPolicy
public RetryPolicy(int maxRetries, long baseDelayMillis, long maxDelayMillis, Set<Integer> retryableStatusCodes, boolean retryOnIoException, boolean honorRetryAfter) Constructor for RetryPolicy that retries only idempotent methods (POST and PATCH are never retried).- Parameters:
maxRetries- maximum retry attempts beyond the initial request (0 disables retries)baseDelayMillis- base backoff delay; the n-th retry waits up tobase * 2^nmaxDelayMillis- ceiling on any single backoff waitretryableStatusCodes- HTTP status codes that trigger a retryretryOnIoException- whether transientIOExceptions are retriedhonorRetryAfter- whether aRetry-Afterresponse header overrides the computed backoff
-
RetryPolicy
public RetryPolicy(int maxRetries, long baseDelayMillis, long maxDelayMillis, Set<Integer> retryableStatusCodes, boolean retryOnIoException, boolean honorRetryAfter, boolean retryNonIdempotentMethods) Full constructor. Also reachable fluently viabuilder()- preferable when configuring by hand, since the positional form lines up twolongdelays and threebooleanflags that are easy to transpose silently:RetryPolicy.builder().maxRetries(5).baseDelayMillis(200).retryableStatusCodes(Set.of(429)).build(). Note the builder applies no defaults beyond Java's zero-values; an unsetretryableStatusCodesis treated as an empty set (no status-triggered retries).- Parameters:
maxRetries- maximum retry attempts beyond the initial request (0 disables retries)baseDelayMillis- base backoff delay; the n-th retry waits up tobase * 2^nmaxDelayMillis- ceiling on any single backoff waitretryableStatusCodes- HTTP status codes that trigger a retryretryOnIoException- whether transientIOExceptions are retriedhonorRetryAfter- whether aRetry-Afterresponse header overrides the computed backoffretryNonIdempotentMethods- whether non-idempotent methods (POST, PATCH) are also retried - leavefalseunless your endpoints dedupe retried requests
-
-
Method Details
-
isRetryableMethod
Whether a request with the given HTTP method is eligible for retry of a failure that may have had server-side effects (a 5xx response or a transportIOException). POST and PATCH are treated as non-idempotent (per RFC 5789 a PATCH - such as the SDK's RFC 6902addoperations - may not be safely re-applied) and are not retried unlessretryNonIdempotentMethodsis set. This gate does not apply to 429 responses - seeisRetryable(int, HttpMethod).- Parameters:
method- the request method (may benull, treated as retryable)- Returns:
trueif retries are permitted for this method
-
isRetryable
Whether a response with the given status code should be retried for a request with the given method. A 429 (Too Many Requests) means the server explicitly did not process the request, so re-sending it can never duplicate a side effect - 429 is retryable for every method (including POST and PATCH), provided 429 is in the retryable status set. All other retryable statuses (the 5xx family) remain gated onisRetryableMethod(HttpMethod).- Parameters:
statusCode- the HTTP status code of the failed attemptmethod- the request method (may benull, treated as retryable)- Returns:
trueif a retry is permitted
-
defaultPolicy
The default policy: up to 3 retries on 429/500/502/503/504 and transient IO errors, 500ms base backoff capped at 20s, withRetry-Afterhonored.- Returns:
- the default policy
-
none
-
getMaxRetries
public int getMaxRetries()- Returns:
- the maximum number of retries
-
isRetryableStatus
public boolean isRetryableStatus(int statusCode) Whether a response with the given status code should be retried.- Parameters:
statusCode- the HTTP status code- Returns:
trueif retryable
-
isRetryOnIoException
public boolean isRetryOnIoException()Whether transient IO exceptions are retried.
- Returns:
trueif IO exceptions are retried
-
computeBackoffMillis
Computes the backoff wait before the given retry attempt.- Parameters:
attempt- zero-based retry attempt indexretryAfterMillis- the server-providedRetry-Afterin millis, ornull- Returns:
- the number of milliseconds to wait
-