Skip to content

A rust library that implements load balancing algorithms.

License

Notifications You must be signed in to change notification settings

blabla-yy/rsbalancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rsbalancer

A rust library that implements load balancing algorithms.

  • round robin
  • weighted round robin(like nginx)
  • random
  • consistent hashing

Installation

cargo add rsbalancer

Usage

Weighted round robin

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());
    }
}

Consistent hashing

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()
        );
    }
}

About

A rust library that implements load balancing algorithms.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages