Crates.io | load-balancer |
lib.rs | load-balancer |
version | 0.3.1 |
created_at | 2025-09-23 23:54:20.866424+00 |
updated_at | 2025-09-26 00:28:10.198816+00 |
description | A set of load balancers for Rust, supporting IP-based, threshold, interval, limit, random, and simple strategies. |
homepage | |
repository | https://github.com/86maid/load-balancer |
max_upload_size | |
id | 1852246 |
size | 80,368 |
A set of asynchronous load balancers for Rust, supporting multiple strategies:
#[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);
}
}
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