Crates.io | hash_ring |
lib.rs | hash_ring |
version | 0.2.0 |
source | src |
created_at | 2015-01-15 00:43:51.385517 |
updated_at | 2020-12-14 03:13:26.63585 |
description | Consistent Hashing library for Rust |
homepage | https://github.com/mattnenterprise/rust-hash-ring |
repository | https://github.com/mattnenterprise/rust-hash-ring |
max_upload_size | |
id | 790 |
size | 22,703 |
Consistent Hashing library for Rust
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
Just fork it, implement your changes and submit a pull request.
MIT