9 releases

Uses old Rust 2015

0.2.0 Dec 14, 2020
0.1.7 Jan 21, 2018
0.0.6 Oct 3, 2017
0.0.5 Jan 20, 2016
0.0.1 Jan 15, 2015

#738 in Data structures

Download history 10/week @ 2022-11-25 7/week @ 2022-12-02 37/week @ 2022-12-09 10/week @ 2022-12-16 19/week @ 2022-12-23 9/week @ 2022-12-30 13/week @ 2023-01-06 9/week @ 2023-01-13 27/week @ 2023-01-20 23/week @ 2023-01-27 32/week @ 2023-02-03 31/week @ 2023-02-10 43/week @ 2023-02-17 9/week @ 2023-02-24 16/week @ 2023-03-03 13/week @ 2023-03-10

89 downloads per month
Used in 2 crates (via rustygear)

MIT license

14KB
261 lines

rust-hash-ring

Consistent Hashing library for Rust

Crates.io crates.io Crates.io CI Coverage Status

Documentation

Usage

extern crate hash_ring;

use hash_ring::HashRing;
use hash_ring::NodeInfo;

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!(
        "Key: '{}', Node: {}",
        "hello",
        hash_ring.get_node(("hello").to_string()).unwrap()
    );

    println!(
        "Key: '{}', Node: {}",
        "dude",
        hash_ring.get_node(("dude").to_string()).unwrap()
    );

    println!(
        "Key: '{}', Node: {}",
        "martian",
        hash_ring.get_node(("martian").to_string()).unwrap()
    );

    println!(
        "Key: '{}', Node: {}",
        "tardis",
        hash_ring.get_node(("tardis").to_string()).unwrap()
    );

    hash_ring.remove_node(&NodeInfo {
        host: "localhost",
        port: 15329,
    });

    println!(
        "Key: '{}', Node: {}",
        "hello",
        hash_ring.get_node(("hello").to_string()).unwrap()
    );

    hash_ring.add_node(&NodeInfo {
        host: "localhost",
        port: 15329,
    });

    println!(
        "Key: '{}', Node: {}",
        "hello",
        hash_ring.get_node(("hello").to_string()).unwrap()
    );
}

For an example of how to use a custom hash function you can look at examples/custom_hasher.rs

Contributing

Just fork it, implement your changes and submit a pull request.

License

MIT

Dependencies

~145KB