Interface RetryPolicy
- All Known Implementing Classes:
RetryPolicy.CustomRetryPolicy, RetryPolicy.ExponentialBackoff, RetryPolicy.FixedDelay, RetryPolicy.NoRetry
public sealed interface RetryPolicy
permits RetryPolicy.NoRetry, RetryPolicy.FixedDelay, RetryPolicy.ExponentialBackoff, RetryPolicy.CustomRetryPolicy
Retry Policy - Defines retry behavior for cache operations
重试策略 - 定义缓存操作的重试行为
Provides various retry strategies including fixed delay, exponential backoff, and custom implementations.
提供多种重试策略,包括固定延迟、指数退避和自定义实现。
Built-in Policies | 内置策略:
noRetry()- No retry | 不重试fixedDelay(int, Duration)- Fixed delay between retries | 固定延迟重试exponentialBackoff(int, Duration, Duration)- Exponential backoff | 指数退避exponentialBackoffWithJitter(int, Duration, Duration)- With random jitter | 带随机抖动
Usage Examples | 使用示例:
// No retry
RetryPolicy noRetry = RetryPolicy.noRetry();
// Fixed delay: 3 retries, 100ms between each
RetryPolicy fixed = RetryPolicy.fixedDelay(3, Duration.ofMillis(100));
// Exponential backoff: 5 retries, 100ms initial, 10s max
RetryPolicy exponential = RetryPolicy.exponentialBackoff(5,
Duration.ofMillis(100), Duration.ofSeconds(10));
// With jitter to prevent thundering herd
RetryPolicy withJitter = RetryPolicy.exponentialBackoffWithJitter(5,
Duration.ofMillis(100), Duration.ofSeconds(10));
// Custom: only retry on specific exceptions
RetryPolicy custom = RetryPolicy.exponentialBackoff(3,
Duration.ofMillis(100), Duration.ofSeconds(5))
.retryOn(ex -> ex instanceof java.io.IOException);
Features | 主要功能:
- Fixed delay retry - 固定延迟重试
- Exponential backoff - 指数退避
- Jitter support - 抖动支持
- Exception-based filtering - 基于异常的过滤
Security | 安全性:
- Thread-safe: Yes (sealed interface, immutable implementations) - 线程安全: 是(密封接口,不可变实现)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-cache V1.9.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classCustom retry policy implementation - wraps a delegate with custom exception filter 自定义重试策略实现 - 用自定义异常过滤器包装委托static final classExponential backoff implementationstatic final classFixed delay implementationstatic final classNo retry implementation -
Method Summary
Modifier and TypeMethodDescriptionstatic RetryPolicyexponentialBackoff(int maxRetries, Duration initialDelay, Duration maxDelay) Exponential backoff retry policy 指数退避重试策略static RetryPolicyexponentialBackoffWithJitter(int maxRetries, Duration initialDelay, Duration maxDelay) Exponential backoff with jitter 带抖动的指数退避static RetryPolicyfixedDelay(int maxRetries, Duration delay) Fixed delay retry policy 固定延迟重试策略getDelay(int attempt) Calculate delay before the next retry attempt 计算下次重试前的延迟intGet the maximum number of retry attempts 获取最大重试次数static RetryPolicynoRetry()No retry policy 不重试策略default RetryPolicyCreate a policy that only retries on specific exceptions 创建仅在特定异常时重试的策略default booleanshouldRetry(Throwable exception) Check if the exception should trigger a retry 检查异常是否应触发重试
-
Method Details
-
maxRetries
int maxRetries()Get the maximum number of retry attempts 获取最大重试次数- Returns:
- max retries (0 means no retry) | 最大重试次数(0 表示不重试)
-
getDelay
Calculate delay before the next retry attempt 计算下次重试前的延迟- Parameters:
attempt- current attempt number (1-based) | 当前尝试次数(从 1 开始)- Returns:
- delay before retry | 重试前的延迟
-
shouldRetry
Check if the exception should trigger a retry 检查异常是否应触发重试- Parameters:
exception- the exception | 异常- Returns:
- true if should retry | 应重试返回 true
-
retryOn
Create a policy that only retries on specific exceptions 创建仅在特定异常时重试的策略- Parameters:
predicate- exception predicate | 异常判断条件- Returns:
- new policy with exception filter | 带异常过滤的新策略
-
noRetry
-
fixedDelay
Fixed delay retry policy 固定延迟重试策略- Parameters:
maxRetries- maximum retry attempts | 最大重试次数delay- delay between retries | 重试间隔- Returns:
- fixed delay policy | 固定延迟策略
-
exponentialBackoff
Exponential backoff retry policy 指数退避重试策略Delay doubles after each attempt up to maxDelay.
每次尝试后延迟翻倍,直到 maxDelay。
- Parameters:
maxRetries- maximum retry attempts | 最大重试次数initialDelay- initial delay | 初始延迟maxDelay- maximum delay cap | 最大延迟上限- Returns:
- exponential backoff policy | 指数退避策略
-
exponentialBackoffWithJitter
static RetryPolicy exponentialBackoffWithJitter(int maxRetries, Duration initialDelay, Duration maxDelay) Exponential backoff with jitter 带抖动的指数退避Adds random jitter (0-50% of delay) to prevent thundering herd.
添加随机抖动(延迟的 0-50%)以防止惊群效应。
- Parameters:
maxRetries- maximum retry attempts | 最大重试次数initialDelay- initial delay | 初始延迟maxDelay- maximum delay cap | 最大延迟上限- Returns:
- policy with jitter | 带抖动的策略
-