Class TokenBucketRetryCondition
- java.lang.Object
-
- software.amazon.awssdk.core.retry.conditions.TokenBucketRetryCondition
-
- All Implemented Interfaces:
RetryCondition
public class TokenBucketRetryCondition extends Object implements RetryCondition
ARetryCondition
that limits the number of retries made by the SDK using a token bucket algorithm. "Tokens" are acquired from the bucket whenevershouldRetry(software.amazon.awssdk.core.retry.RetryPolicyContext)
returns true, and are released to the bucket wheneverrequestSucceeded(software.amazon.awssdk.core.retry.RetryPolicyContext)
orrequestWillNotBeRetried(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 theRetryPolicy.Builder.retryCapacityCondition(software.amazon.awssdk.core.retry.conditions.RetryCondition)
to null.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TokenBucketRetryCondition.Builder
Configure and create aTokenBucketRetryCondition
.static class
TokenBucketRetryCondition.Capacity
The number of tokens in the token bucket after a specific token acquisition succeeds.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 theRetryMode.defaultRetryMode()
.boolean
equals(Object o)
static TokenBucketRetryCondition
forRetryMode(RetryMode retryMode)
Create a condition using the configuredRetryMode
.static Optional<TokenBucketRetryCondition.Capacity>
getCapacityForExecution(ExecutionAttributes attributes)
IfshouldRetry(RetryPolicyContext)
returned true for the provided execution, this method returns theTokenBucketRetryCondition.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()
-
-
-
Method Detail
-
create
public static TokenBucketRetryCondition create()
Create a condition using theRetryMode.defaultRetryMode()
. This is equivalent toforRetryMode(RetryMode.defaultRetryMode())
.For more detailed control, see
builder()
.
-
forRetryMode
public static TokenBucketRetryCondition forRetryMode(RetryMode retryMode)
Create a condition using the configuredRetryMode
. TheRetryMode.LEGACY
does not subtract tokens from the token bucket when throttling exceptions are encountered. TheRetryMode.STANDARD
treats throttling and non- throttling exceptions as the same cost.For more detailed control, see
builder()
.
-
builder
public static TokenBucketRetryCondition.Builder builder()
Create a builder that allows fine-grained control over the token policy of this condition.
-
getCapacityForExecution
public static Optional<TokenBucketRetryCondition.Capacity> getCapacityForExecution(ExecutionAttributes attributes)
IfshouldRetry(RetryPolicyContext)
returned true for the provided execution, this method returns theTokenBucketRetryCondition.Capacity
consumed by the request.
-
tokensAvailable
public int tokensAvailable()
Retrieve the number of tokens currently available in the token bucket. This is a volatile snapshot of the current value. SeegetCapacityForExecution(ExecutionAttributes)
to see how much capacity was left in the bucket after a specific execution was considered.
-
shouldRetry
public boolean shouldRetry(RetryPolicyContext context)
Description copied from interface:RetryCondition
Determine whether a request should or should not be retried.- Specified by:
shouldRetry
in interfaceRetryCondition
- Parameters:
context
- Context about the state of the last request and information about the number of requests made.- Returns:
- True if the request should be retried, false if not.
-
requestWillNotBeRetried
public void requestWillNotBeRetried(RetryPolicyContext context)
Description copied from interface:RetryCondition
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.- Specified by:
requestWillNotBeRetried
in interfaceRetryCondition
-
requestSucceeded
public void requestSucceeded(RetryPolicyContext context)
Description copied from interface:RetryCondition
Called by the SDK to notify this condition that the provided request succeeded. This method is invoked even if the execution never failed before (RetryPolicyContext.retriesAttempted()
is zero).- Specified by:
requestSucceeded
in interfaceRetryCondition
-
-