Class CircuitBreaker
java.lang.Object
cloud.opencode.base.cache.protection.CircuitBreaker
Circuit Breaker - Cascade failure prevention for cache backends
熔断器 - 防止缓存后端级联故障
Protects cache backends from cascade failures by failing fast when error rate is high.
当错误率过高时快速失败,保护缓存后端免受级联故障。
Features | 主要功能:
- Three states: CLOSED, OPEN, HALF_OPEN - 三种状态:关闭、打开、半开
- Failure threshold triggering - 失败阈值触发
- Automatic recovery - 自动恢复
- Fallback support - 降级支持
Usage Examples | 使用示例:
CircuitBreaker breaker = CircuitBreaker.create(
new CircuitBreaker.Config(5, Duration.ofSeconds(30), 3, 0.5));
User user = breaker.execute(
() -> loadFromDb(key),
() -> loadFromCache(key)); // fallback
Performance | 性能特性:
- Time complexity: O(1) state check - 时间复杂度: O(1) 状态检查
- Space complexity: O(1) - 空间复杂度: O(1)
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-cache V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classException thrown when circuit breaker is open 熔断器打开时抛出的异常static final recordCircuit breaker configuration 熔断器配置static enumCircuit breaker state 熔断器状态 -
Method Summary
Modifier and TypeMethodDescriptionbooleanCheck if request is allowed through circuit breaker 检查是否允许请求通过熔断器voidclose()Manually close circuit 手动关闭熔断器static CircuitBreakercreate()Create circuit breaker with defaults 使用默认配置创建熔断器static CircuitBreakercreate(CircuitBreaker.Config config) Create circuit breaker with config 使用配置创建熔断器<T> TExecute operation with circuit breaker protection 在熔断器保护下执行操作<T> TExecute operation with fallback 执行操作并带降级<T> CompletableFuture<T> executeAsync(Supplier<CompletableFuture<T>> supplier) Execute async operation 异步执行操作getState()Get current state 获取当前状态voidopen()Manually open circuit 手动打开熔断器voidRecord a failed operation 记录失败的操作voidRecord a successful operation 记录成功的操作voidreset()Reset statistics 重置统计
-
Method Details
-
create
Create circuit breaker with config 使用配置创建熔断器- Parameters:
config- the config | 配置- Returns:
- circuit breaker | 熔断器
-
create
Create circuit breaker with defaults 使用默认配置创建熔断器- Returns:
- circuit breaker | 熔断器
-
execute
Execute operation with circuit breaker protection 在熔断器保护下执行操作- Type Parameters:
T- result type | 结果类型- Parameters:
supplier- the operation | 操作- Returns:
- result | 结果
- Throws:
CircuitBreaker.CircuitBreakerOpenException- if circuit is open | 熔断器打开时抛出异常
-
execute
-
executeAsync
Execute async operation 异步执行操作- Type Parameters:
T- result type | 结果类型- Parameters:
supplier- the async operation | 异步操作- Returns:
- future containing result | 包含结果的 Future
-
getState
-
open
public void open()Manually open circuit 手动打开熔断器 -
close
public void close()Manually close circuit 手动关闭熔断器 -
reset
public void reset()Reset statistics 重置统计 -
allowRequest
public boolean allowRequest()Check if request is allowed through circuit breaker 检查是否允许请求通过熔断器- Returns:
- true if allowed | 允许返回 true
-
recordSuccess
public void recordSuccess()Record a successful operation 记录成功的操作 -
recordFailure
public void recordFailure()Record a failed operation 记录失败的操作
-