Module load_balancer

Module load_balancer 

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.

§Load Balancer Module

This module provides a robust load balancer implementation for distributing incoming requests across multiple backend servers. It supports various load balancing strategies and includes health checking, connection management, and detailed statistics.

§Key Components

  • DMSCLoadBalancerStrategy: Enum representing different load balancing algorithms
  • DMSCBackendServer: Represents a backend server with configuration and health status
  • DMSCLoadBalancer: Main load balancer implementation
  • DMSCLoadBalancerServerStats: Metrics for monitoring server performance

§Design Principles

  1. Multiple Strategies: Supports RoundRobin, WeightedRoundRobin, LeastConnections, Random, and IpHash
  2. Health Checking: Automatic periodic health checks to ensure traffic is only sent to healthy servers
  3. Connection Management: Tracks active connections and enforces max connections per server
  4. Detailed Statistics: Collects metrics on requests, failures, and response times
  5. Thread Safety: Uses Arc and RwLock for safe operation in multi-threaded environments
  6. Scalability: Designed to handle large numbers of servers and requests
  7. Configurable: Allows fine-tuning of server weights, max connections, and health check paths
  8. Async Compatibility: Built with async/await patterns for modern Rust applications

§Usage

use dmsc::prelude::*;
use std::sync::Arc;
 
async fn example() -> DMSCResult<()> {
    // Create a load balancer with Round Robin strategy
    let lb = Arc::new(DMSCLoadBalancer::new(DMSCLoadBalancerStrategy::RoundRobin));
     
    // Add backend servers
    lb.add_server(DMSCBackendServer::new("server1".to_string(), "http://localhost:8081".to_string())
        .with_weight(2)
        .with_max_connections(200))
        .await;
     
    lb.add_server(DMSCBackendServer::new("server2".to_string(), "http://localhost:8082".to_string())
        .with_weight(1)
        .with_max_connections(100))
        .await;
     
    // Start periodic health checks every 30 seconds
    lb.clone().start_health_checks(30).await;
     
    // Select a server for a client request
    let server = lb.select_server(Some("192.168.1.1")).await?;
    println!("Selected server: {}", server.url);
     
    // Record response time when done
    lb.record_response_time(&server.id, 150).await;
     
    // Release the server when the request is complete
    lb.release_server(&server.id).await;
     
    // Get server statistics
    let stats = lb.get_all_stats().await;
    println!("Server stats: {:?}", stats);
     
    Ok(())
}

Structs§

DMSCBackendServer
Represents a backend server in the load balancer.
DMSCLoadBalancer
Main load balancer implementation.
DMSCLoadBalancerServerStats
Load balancer server statistics for monitoring and reporting.

Enums§

DMSCLoadBalancerStrategy
Load balancing strategies supported by DMSC.