Module circuit_breaker

Module circuit_breaker 

Source
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.0

Unless 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

  1. Fault Isolation: Prevent cascading failures by stopping requests to failing services
  2. Automatic Recovery: Automatically test and recover when services become healthy again
  3. Configurable Behavior: Allow fine-tuning of failure thresholds, timeouts, and recovery parameters
  4. Metrics Collection: Track and report circuit breaker performance for monitoring
  5. Thread Safety: Ensure safe operation in multi-threaded environments
  6. Error Type Specificity: Advanced implementation supports different thresholds for different error types
  7. 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§

DMSCAdvancedCircuitBreaker
Advanced circuit breaker with separate failure thresholds for different error types.
DMSCCircuitBreaker
Basic circuit breaker implementation.
DMSCCircuitBreakerConfig
Configuration for circuit breaker behavior.
DMSCCircuitBreakerMetrics
Metrics for monitoring circuit breaker performance.

Enums§

DMSCCircuitBreakerState
Represents the three states of a circuit breaker.