Crates.io | rsbalancer |
lib.rs | rsbalancer |
version | 0.3.0 |
source | src |
created_at | 2023-06-29 04:00:58.846089 |
updated_at | 2023-08-15 14:16:53.184093 |
description | A rust library that implements load balancing algorithms. |
homepage | https://github.com/blabla-yy/rsbalancer |
repository | https://github.com/blabla-yy/rsbalancer |
max_upload_size | |
id | 902937 |
size | 31,844 |
A rust library that implements load balancing algorithms.
cargo add rsbalancer
use rsbalancer::Node;
fn main() {
let mut balancer = rsbalancer::weighted_round_robin(vec![
Node::new("ip1", 1), // ip、weight
Node::new("ip2", 1),
Node::new("ip3", 2),
]);
for _ in 0..10 {
println!("{}", balancer.next_id().unwrap());
}
}
use rsbalancer::Node;
fn main() {
// number of virtual nodes = node.weight * replicas
let balancer = rsbalancer::consistent_hashing(
vec![
Node::new("ip1".to_string(), 1), // ip、weight
Node::new("ip2".to_string(), 1),
Node::new("ip3".to_string(), 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()
);
}
}