A rust library that implements load balancing algorithms.
shell
cargo add rsbalancer
```rust use rsbalancer::Node;
fn main() { let mut balancer = rsbalancer::weightedroundrobin(vec![ Node::new("ip1", 1), // ip、weight Node::new("ip2", 1), Node::new("ip3", 2), ]);
for _ in 0..10 {
println!("{}", balancer.next_id().unwrap());
}
} ```
```rust use rsbalancer::Node;
fn main() { // number of virtual nodes = node.weight * replicas let balancer = rsbalancer::consistenthashing( vec![ Node::new("ip1".tostring(), 1), // ip、weight Node::new("ip2".tostring(), 1), Node::new("ip3".tostring(), 1), ], 160, //replicas );
for random_ip in 0..10 {
println!(
"{} == {}",
balancer
.get_matching_node_id(&random_ip.to_string())
.unwrap(),
balancer
.get_matching_node(&random_ip.to_string())
.unwrap()
.get_id()
);
}
}
```