load-balancer

Crates.ioload-balancer
lib.rsload-balancer
version0.3.1
created_at2025-09-23 23:54:20.866424+00
updated_at2025-09-26 00:28:10.198816+00
descriptionA set of load balancers for Rust, supporting IP-based, threshold, interval, limit, random, and simple strategies.
homepage
repositoryhttps://github.com/86maid/load-balancer
max_upload_size
id1852246
size80,368
(86maid)

documentation

README

load-balancer

A set of asynchronous load balancers for Rust, supporting multiple strategies:

  • IP-based: bind clients to system IP addresses.
  • Threshold: limit retries based on errors.
  • Limit: restrict usage per interval.
  • Random: pick entries randomly.
  • Simple: round-robin sequential allocation.
  • Interval: interval allocation.

Examples

#[tokio::main]
async fn main() {
    // Each node can be used at most 2 times, interval 1 second
    let lb = LimitLoadBalancer::new(vec![(2, "node 1"), (2, "node 2")]);
    let start = tokio::time::Instant::now();

    for _ in 0..8 {
        let node = lb.alloc().await;

        println!("{}s Allocated node: {}", start.elapsed().as_secs(), node);
    }

    println!("------------------------------");

    // Each node can be used at most 4 times, interval 5 second
    let lb = LimitLoadBalancer::new_interval(
        vec![(3, "node 1"), (1, "node 2")],
        Duration::from_secs(5),
    );

    let start = tokio::time::Instant::now();

    for _ in 0..8 {
        let node = lb.alloc().await;

        println!("{}s Allocated node: {}", start.elapsed().as_secs(), node);
    }
}

Output

0s Allocated node: node 1
0s Allocated node: node 1
0s Allocated node: node 2
0s Allocated node: node 2
1s Allocated node: node 1
1s Allocated node: node 1     
1s Allocated node: node 2     
1s Allocated node: node 2     
------------------------------
0s Allocated node: node 1     
0s Allocated node: node 1     
0s Allocated node: node 1     
0s Allocated node: node 2     
5s Allocated node: node 1
5s Allocated node: node 1
5s Allocated node: node 1
5s Allocated node: node 2
Commit count: 8

cargo fmt