public class TokenBucketRetryCondition extends Object implements RetryCondition
RetryCondition
that limits the number of retries made by the SDK using a token bucket algorithm. "Tokens" are
acquired from the bucket whenever shouldRetry(software.amazon.awssdk.core.retry.RetryPolicyContext)
returns true, and are released to the bucket whenever
requestSucceeded(software.amazon.awssdk.core.retry.RetryPolicyContext)
or requestWillNotBeRetried(software.amazon.awssdk.core.retry.RetryPolicyContext)
are invoked.
If "tokens" cannot be acquired from the bucket, it means too many requests have failed and the request will not be allowed
to retry until we start to see initial non-retried requests succeed via requestSucceeded(RetryPolicyContext)
.
This prevents the client from holding the calling thread to retry when it's likely that it will fail anyway.
This is currently included in the default RetryPolicy.aggregateRetryCondition()
, but can be disabled by setting the
RetryPolicy.Builder#retryCapacityCondition
to null.
Modifier and Type | Class and Description |
---|---|
static class |
TokenBucketRetryCondition.Builder
Configure and create a
TokenBucketRetryCondition . |
static class |
TokenBucketRetryCondition.Capacity
The number of tokens in the token bucket after a specific token acquisition succeeds.
|
Modifier and Type | Method and Description |
---|---|
static TokenBucketRetryCondition.Builder |
builder()
Create a builder that allows fine-grained control over the token policy of this condition.
|
static TokenBucketRetryCondition |
create()
Create a condition using the
RetryMode.defaultRetryMode() . |
boolean |
equals(Object o) |
static TokenBucketRetryCondition |
forRetryMode(RetryMode retryMode)
Create a condition using the configured
RetryMode . |
static Optional<TokenBucketRetryCondition.Capacity> |
getCapacityForExecution(ExecutionAttributes attributes)
If
shouldRetry(RetryPolicyContext) returned true for the provided execution, this method returns the
TokenBucketRetryCondition.Capacity consumed by the request. |
int |
hashCode() |
void |
requestSucceeded(RetryPolicyContext context)
Called by the SDK to notify this condition that the provided request succeeded.
|
void |
requestWillNotBeRetried(RetryPolicyContext context)
Called by the SDK to notify this condition that the provided request will not be retried, because some retry condition
determined that it shouldn't be retried.
|
boolean |
shouldRetry(RetryPolicyContext context)
Determine whether a request should or should not be retried.
|
int |
tokensAvailable()
Retrieve the number of tokens currently available in the token bucket.
|
String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
defaultRetryCondition, none
public static TokenBucketRetryCondition create()
RetryMode.defaultRetryMode()
. This is equivalent to
forRetryMode(RetryMode.defaultRetryMode())
.
For more detailed control, see builder()
.
public static TokenBucketRetryCondition forRetryMode(RetryMode retryMode)
RetryMode
. The RetryMode.LEGACY
does not subtract tokens from
the token bucket when throttling exceptions are encountered. The RetryMode.STANDARD
treats throttling and non-
throttling exceptions as the same cost.
For more detailed control, see builder()
.
public static TokenBucketRetryCondition.Builder builder()
public static Optional<TokenBucketRetryCondition.Capacity> getCapacityForExecution(ExecutionAttributes attributes)
shouldRetry(RetryPolicyContext)
returned true for the provided execution, this method returns the
TokenBucketRetryCondition.Capacity
consumed by the request.public int tokensAvailable()
getCapacityForExecution(ExecutionAttributes)
to see how much capacity was left in the bucket after a specific
execution was considered.public boolean shouldRetry(RetryPolicyContext context)
RetryCondition
shouldRetry
in interface RetryCondition
context
- Context about the state of the last request and information about the number of requests made.public void requestWillNotBeRetried(RetryPolicyContext context)
RetryCondition
requestWillNotBeRetried
in interface RetryCondition
public void requestSucceeded(RetryPolicyContext context)
RetryCondition
RetryPolicyContext.retriesAttempted()
is zero).requestSucceeded
in interface RetryCondition
Copyright © 2023. All rights reserved.