← Timeout | Retry(中文) | Error Handling →
Default
autoRetry:trueminRetryDelayMs:300maxRetryDelayMs:300 * 1000retryCondition:com.volcengine.retryer.DefaultRetryConditionbackoffStrategy:com.volcengine.retryer.ExponentialBackoffStrategyretryErrorCode: empty set
The SDK retries on network errors and throttling. Business logic errors are not retried.
import com.volcengine.ApiClient;
import com.volcengine.retryer.DefaultRetryCondition;
import com.volcengine.retryer.ExponentialBackoffStrategy;
import com.volcengine.sign.Credentials;
public class SampleCode {
public static void main(String[] args) {
String region = "cn-beijing";
ApiClient apiClient = new ApiClient()
.setCredentials(Credentials.getEnvCredentials())
.setRegion(region)
.setDebugging(true)
.setAutoRetry(true)
.setNumMaxRetries(3)
.setMinRetryDelayMs(1000)
.setMaxRetryDelayMs(3000)
.setRetryCondition(new DefaultRetryCondition())
.setBackoffStrategy(new ExponentialBackoffStrategy())
.addRetryErrorCode("InvalidAuthorization");
}
}Retry conditions define which situations should trigger a retry. The SDK includes a default retry condition, and users can also customize retry conditions based on their business needs.
The default retry condition DefaultRetryCondition includes the following:
- Retry on network errors.
- Retry on server throttling errors.
- Retry on user-specified error codes (
retryErrorCodes).
Users can customize retry conditions based on their business needs.
Code Example:
-
Extend the base class
RetryConditionand implementboolean shouldRetry(ApiResponse response, Exception error):import com.volcengine.retryer.RetryCondition; public abstract class CustomRetryCondition extends RetryCondition { @Override public boolean shouldRetry(ApiResponse response, Exception error) { // Implement your own logic return false; } }
-
Reuse the default
DefaultRetryConditionlogic:import com.volcengine.retryer.DefaultRetryCondition; public class CustomRetryCondition extends DefaultRetryCondition{ @Override public boolean shouldRetry(ApiResponse response, Exception error) { boolean shouldRetry = super.shouldRetry(response, error); // Implement your own logic return false; } }
The backoff strategy defines the delay between each retry attempt. The SDK includes built-in backoff strategies, and users can also customize their own based on their needs.
Default
ExponentialWithRandomJitterBackoffStrategy
Boundary values: minRetryDelay (min delay), maxRetryDelay (max delay), n is the retry count.
| Name | Description | Formula |
|---|---|---|
NoBackoffStrategy |
No backoff, retry immediately. | delay = 0.0 |
ExponentialBackoffStrategy |
Exponential backoff with bounds. | delay = min(minRetryDelay * 2ⁿ, maxRetryDelay) |
ExponentialWithRandomJitterBackoffStrategy |
Exponential with jitter; value lies in [base, 2·base]. |
base = min(minRetryDelay · 2ⁿ, maxRetryDelay)delay = base + U(0, base) |
Users can customize their own backoff strategy based on their needs.
Code Example:
-
Extend the base class
BackoffStrategyand implementpublic long computeDelay(int retryCount):import com.volcengine.retryer.BackoffStrategy; class CustomBackoffStrategy extends BackoffStrategy { public long computeDelay(int retryCount){ long minRetryDelayMs = this.minRetryDelayMs; long maxRetryDelayMs = this.maxRetryDelayMs; // Implement your own logic return 0; } }
-
You can also reuse a built-in backoff strategy such as
ExponentialBackoffStrategyorExponentialWithRandomJitterBackoffStrategy:import com.volcengine.retryer.ExponentialWithDecayBackoffStrategy; class CustomBackoffStrategy extends ExponentialWithDecayBackoffStrategy{ public long computeDelay(int retryCount){ long base = super.computeDelay(retryCount); // Call the parent implementation long minRetryDelayMs = this.minRetryDelayMs; long maxRetryDelayMs = this.maxRetryDelayMs; // Implement your own logic return 0; } }
← Timeout | Retry(中文) | Error Handling →