Expand description
Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
This file is part of DMSC. The DMSC project belongs to the Dunimd Team.
Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
§Circuit Breaker Module
This module provides robust circuit breaker implementations for fault tolerance in distributed systems. Circuit breakers prevent cascading failures by temporarily stopping requests to failing services.
§Key Components
- DMSCCircuitBreakerState: Enum representing the three states of a circuit breaker (Closed, Open, HalfOpen)
- DMSCCircuitBreakerConfig: Configuration for circuit breaker behavior
- DMSCCircuitBreaker: Basic circuit breaker implementation
- DMSCAdvancedCircuitBreaker: Advanced circuit breaker with error-type specific thresholds
- DMSCCircuitBreakerMetrics: Metrics for monitoring circuit breaker performance
§Design Principles
- Fault Isolation: Prevent cascading failures by stopping requests to failing services
- Automatic Recovery: Automatically test and recover when services become healthy again
- Configurable Behavior: Allow fine-tuning of failure thresholds, timeouts, and recovery parameters
- Metrics Collection: Track and report circuit breaker performance for monitoring
- Thread Safety: Ensure safe operation in multi-threaded environments
- Error Type Specificity: Advanced implementation supports different thresholds for different error types
- Async Compatibility: Designed for use with async/await patterns
§Usage
use dmsc::prelude::*;
async fn example() -> DMSCResult<()> {
// Create a circuit breaker with default configuration
let cb_config = DMSCCircuitBreakerConfig::default();
let cb = DMSCCircuitBreaker::new(cb_config);
// Execute a risky operation with circuit breaker protection
let result = cb.execute(async || {
// This could be a network request, database operation, etc.
Ok("Success!")
}).await;
// Get circuit breaker state and metrics
let state = cb.get_state().await;
let metrics = cb.get_stats();
println!("Circuit breaker state: {:?}", state);
println!("Circuit breaker metrics: {:?}", metrics);
Ok(())
}Structs§
- DMSC
Advanced Circuit Breaker - Advanced circuit breaker with separate failure thresholds for different error types.
- DMSC
Circuit Breaker - Basic circuit breaker implementation.
- DMSC
Circuit Breaker Config - Configuration for circuit breaker behavior.
- DMSC
Circuit Breaker Metrics - Metrics for monitoring circuit breaker performance.
Enums§
- DMSC
Circuit Breaker State - Represents the three states of a circuit breaker.