Interface BackoffStrategy
- All Known Implementing Classes:
BackoffStrategy.Exponential, BackoffStrategy.ExponentialWithJitter, BackoffStrategy.Fibonacci, BackoffStrategy.Fixed
public sealed interface BackoffStrategy
permits BackoffStrategy.Fixed, BackoffStrategy.Exponential, BackoffStrategy.ExponentialWithJitter, BackoffStrategy.Fibonacci
BackoffStrategy - Defines delay calculation between retry attempts
退避策略 - 定义重试之间的延迟计算方式
Sealed interface with built-in implementations: Fixed, Exponential, ExponentialWithJitter, and Fibonacci.
密封接口,内置实现:固定延迟、指数退避、带抖动的指数退避、斐波那契退避。
- Since:
- JDK 25, opencode-base-core V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordExponential backoff - delay grows by a multiplier each attempt.static final recordExponential backoff with jitter - adds randomness to prevent thundering herd.static final recordFibonacci backoff - delay follows the Fibonacci sequence.static final recordFixed backoff - returns the same delay for every attempt. -
Method Summary
Modifier and TypeMethodDescriptiondelay(int attempt) Calculate the delay for the given attempt number.static BackoffStrategyexponential(Duration initialDelay, double multiplier) Create an exponential backoff strategy.static BackoffStrategyexponentialWithJitter(Duration initialDelay, double multiplier, double jitterFactor) Create an exponential backoff strategy with jitter.static BackoffStrategyCreate a Fibonacci backoff strategy.static BackoffStrategyCreate a fixed backoff strategy.
-
Method Details
-
delay
Calculate the delay for the given attempt number. 计算给定重试次数的延迟。- Parameters:
attempt- attempt number, starting from 1 - 重试次数,从1开始- Returns:
- the delay duration - 延迟时长
-
fixed
Create a fixed backoff strategy. 创建固定退避策略。- Parameters:
delay- the fixed delay between retries - 重试之间的固定延迟- Returns:
- a fixed backoff strategy - 固定退避策略
-
exponential
Create an exponential backoff strategy. 创建指数退避策略。- Parameters:
initialDelay- the initial delay - 初始延迟multiplier- the multiplier applied per attempt - 每次重试的乘数- Returns:
- an exponential backoff strategy - 指数退避策略
-
exponentialWithJitter
static BackoffStrategy exponentialWithJitter(Duration initialDelay, double multiplier, double jitterFactor) Create an exponential backoff strategy with jitter. 创建带抖动的指数退避策略。- Parameters:
initialDelay- the initial delay - 初始延迟multiplier- the multiplier applied per attempt - 每次重试的乘数jitterFactor- jitter factor in [0.0, 1.0] - 抖动因子,范围 [0.0, 1.0]- Returns:
- an exponential-with-jitter backoff strategy - 带抖动的指数退避策略
-
fibonacci
Create a Fibonacci backoff strategy. 创建斐波那契退避策略。- Parameters:
initialDelay- the initial delay (used as the first two Fibonacci values) - 初始延迟(作为前两个斐波那契值)- Returns:
- a Fibonacci backoff strategy - 斐波那契退避策略
-