Interface RetryPolicy

All Known Implementing Classes:
RetryPolicy.CustomRetryPolicy, RetryPolicy.ExponentialBackoff, RetryPolicy.FixedDelay, RetryPolicy.NoRetry

Retry Policy - Defines retry behavior for cache operations 重试策略 - 定义缓存操作的重试行为

Provides various retry strategies including fixed delay, exponential backoff, and custom implementations.

提供多种重试策略,包括固定延迟、指数退避和自定义实现。

Built-in Policies | 内置策略:

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:
  • Method Details

    • maxRetries

      int maxRetries()
      Get the maximum number of retry attempts 获取最大重试次数
      Returns:
      max retries (0 means no retry) | 最大重试次数(0 表示不重试)
    • getDelay

      Duration getDelay(int attempt)
      Calculate delay before the next retry attempt 计算下次重试前的延迟
      Parameters:
      attempt - current attempt number (1-based) | 当前尝试次数(从 1 开始)
      Returns:
      delay before retry | 重试前的延迟
    • shouldRetry

      default boolean shouldRetry(Throwable exception)
      Check if the exception should trigger a retry 检查异常是否应触发重试
      Parameters:
      exception - the exception | 异常
      Returns:
      true if should retry | 应重试返回 true
    • retryOn

      default RetryPolicy retryOn(Predicate<Throwable> predicate)
      Create a policy that only retries on specific exceptions 创建仅在特定异常时重试的策略
      Parameters:
      predicate - exception predicate | 异常判断条件
      Returns:
      new policy with exception filter | 带异常过滤的新策略
    • noRetry

      static RetryPolicy noRetry()
      No retry policy 不重试策略
      Returns:
      no retry policy | 不重试策略
    • fixedDelay

      static RetryPolicy fixedDelay(int maxRetries, Duration delay)
      Fixed delay retry policy 固定延迟重试策略
      Parameters:
      maxRetries - maximum retry attempts | 最大重试次数
      delay - delay between retries | 重试间隔
      Returns:
      fixed delay policy | 固定延迟策略
    • exponentialBackoff

      static RetryPolicy exponentialBackoff(int maxRetries, Duration initialDelay, Duration maxDelay)
      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 | 带抖动的策略