Module traffic_management

Module traffic_management 

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.

§Traffic Management Module

This module provides traffic management functionality for the DMSC service mesh. It allows configuring and managing traffic routes, traffic splits, circuit breakers, rate limits, and fault injection for services in the mesh.

§Key Components

  • DMSCTrafficRoute: Configuration for routing traffic between services
  • DMSCMatchCriteria: Criteria for matching requests to routes
  • DMSCRouteAction: Action to take for matched requests
  • DMSCWeightedDestination: Weighted destination for traffic splitting
  • DMSCRetryPolicy: Configuration for request retries
  • DMSCFaultInjection: Configuration for fault injection
  • DMSCTrafficSplit: Configuration for splitting traffic between service subsets
  • DMSCSubset: Service subset definition for traffic splitting
  • DMSCTrafficManager: Main traffic management service
  • DMSCCircuitBreakerConfig: Configuration for circuit breakers
  • DMSCRateLimitConfig: Configuration for rate limiting

§Design Principles

  1. Declarative Configuration: Traffic rules are defined declaratively
  2. Flexible Routing: Supports multiple routing actions (route, redirect, direct response)
  3. Traffic Splitting: Weighted traffic splitting between service subsets
  4. Resilience: Built-in retry policies and circuit breakers
  5. Fault Injection: Support for fault injection for testing resilience
  6. Rate Limiting: Protection against excessive traffic
  7. Timeout Management: Configurable request timeouts
  8. Thread-safe: Uses Arc and RwLock for safe concurrent access
  9. Graceful Shutdown: Proper cleanup of background tasks
  10. Extensible: Easy to add new traffic management features

§Usage

use dmsc::prelude::*;
use std::time::Duration;
 
async fn example() -> DMSCResult<()> {
    // Create a traffic manager
    let traffic_manager = DMSCTrafficManager::new(true);
     
    // Create a traffic route
    let route = DMSCTrafficRoute {
        name: "http-route".to_string(),
        source_service: "gateway".to_string(),
        destination_service: "backend".to_string(),
        match_criteria: DMSCMatchCriteria {
            path_prefix: Some("/api".to_string()),
            headers: HashMap::new(),
            method: Some("GET".to_string()),
            query_parameters: HashMap::new(),
        },
        route_action: DMSCRouteAction::Route(vec![DMSCWeightedDestination {
            service: "backend-v1".to_string(),
            weight: 80,
            subset: None,
        }, DMSCWeightedDestination {
            service: "backend-v2".to_string(),
            weight: 20,
            subset: None,
        }]),
        retry_policy: Some(DMSCRetryPolicy {
            attempts: 3,
            per_try_timeout: Duration::from_secs(1),
            retry_on: vec!["5xx".to_string()],
        }),
        timeout: Some(Duration::from_secs(5)),
        fault_injection: None,
    };
     
    // Add the route
    traffic_manager.add_traffic_route(route).await?;
     
    // Set a circuit breaker
    let cb_config = DMSCCircuitBreakerConfig {
        consecutive_errors: 5,
        interval: Duration::from_secs(10),
        base_ejection_time: Duration::from_secs(30),
        max_ejection_percent: 50.0,
    };
    traffic_manager.set_circuit_breaker_config("backend", cb_config).await?;
     
    Ok(())
}

Structs§

DMSCAbortFault
DMSCCircuitBreakerConfig
DMSCDelayFault
DMSCFaultInjection
DMSCMatchCriteria
DMSCRateLimitConfig
DMSCRetryPolicy
DMSCSubset
DMSCTrafficManager
DMSCTrafficRoute
DMSCTrafficSplit
DMSCWeightedDestination

Enums§

DMSCRouteAction