#consistent-hashing #hash-ring #distributed-systems #algorithm #construction #starting #port

nave

Thinking about the construction of distributed systems starting from the *consistent hash* algorithm

1 unstable release

0.1.0 Sep 3, 2020

#1554 in Data structures

MIT/Apache

8KB
123 lines

Nave

Crates.io Docs MIT/APACHE-2.0 GitHub Workflow Status

Thinking about the construction of distributed systems starting from the consistent hash algorithm.

Nave's vision is to become the basis for building a robust and highly available distributed system. The possible final product is a collection of important algorithms or a framework in a certain sense.

You can already use it in projects, of course, currently only a port of rust-hash-ring.

Features

Usage

  • HashRing

    extern crate nave;
    
    use nave::HashRing;
    
    /// Custom Node Info. 
    #[derive(Clone, Debug)]
    struct NodeInfo {
        pub host: &'static str,
        pub port: u16,
    }
    
    /// Impl ToString to format NodeInfo
    impl ToString for NodeInfo {
        fn to_string(&self) -> String {
            format!("{}:{}", self.host, self.port)
        }
    }
    
    fn main() {
        let mut nodes: Vec<NodeInfo> = Vec::new();
        nodes.push(NodeInfo { host: "localhost", port: 15324 });
        nodes.push(NodeInfo { host: "localhost", port: 15325 });
        nodes.push(NodeInfo { host: "localhost", port: 15326 });
        nodes.push(NodeInfo { host: "localhost", port: 15327 });
        nodes.push(NodeInfo { host: "localhost", port: 15328 });
        nodes.push(NodeInfo { host: "localhost", port: 15329 });
    
        let mut hash_ring: HashRing<NodeInfo> = HashRing::new(nodes, 10);
    
        println!("{}", hash_ring.get_node(("hello").to_string()).unwrap().to_string());
    
        println!("{}", hash_ring.get_node(("dude").to_string()).unwrap().to_string());
    
        println!("{}", hash_ring.get_node(("martian").to_string()).unwrap().to_string());
    
        println!("{}", hash_ring.get_node(("tardis").to_string()).unwrap().to_string());
    
        hash_ring.remove_node(&NodeInfo { host: "localhost", port: 15329 });
    
        println!("{}", hash_ring.get_node(("hello").to_string()).unwrap().to_string());
    
        hash_ring.add_node(&NodeInfo { host: "localhost", port: 15329 });
    
        println!("{}", hash_ring.get_node(("hello").to_string()).unwrap().to_string());
    }
    

Contact

Chojan Shang - @PsiACE - psiace@outlook.com

Project Link: https://github.com/psiace/nave

Sponsor

If my work can make you feel happy, you can consider buying me a cup of coffee:

License

Licensed under either of

at your option.

Dependencies

~150KB