https://github.com/mattnenterprise/rust-hash-ring
A consistent hashing library in Rust
https://github.com/mattnenterprise/rust-hash-ring
consistent-hashing consistent-hashing-library hash-ring rust
Last synced: about 1 month ago
JSON representation
A consistent hashing library in Rust
- Host: GitHub
- URL: https://github.com/mattnenterprise/rust-hash-ring
- Owner: mattnenterprise
- License: mit
- Created: 2015-01-14T20:37:15.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2021-10-14T01:25:29.000Z (about 4 years ago)
- Last Synced: 2025-08-16T06:17:34.813Z (5 months ago)
- Topics: consistent-hashing, consistent-hashing-library, hash-ring, rust
- Language: Rust
- Homepage:
- Size: 34.2 KB
- Stars: 33
- Watchers: 1
- Forks: 18
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
rust-hash-ring
================
Consistent Hashing library for Rust
[](https://crates.io/crates/hash_ring)
[](https://crates.io/crates/hash_ring)
[](https://crates.io/crates/hash_ring)
[](https://github.com/mattnenterprise/rust-hash-ring/actions?query=workflow%3ACI)
[](https://app.codecov.io/gh/mattnenterprise/rust-hash-ring/branch/master)
[Documentation](https://docs.rs/hash_ring)
### Usage
```rust
extern crate hash_ring;
use hash_ring::HashRing;
use hash_ring::NodeInfo;
fn main() {
let mut nodes: Vec = 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 = 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](https://github.com/mattnenterprise/rust-hash-ring/blob/master/examples/custom_hasher.rs)
### Contributing
Just fork it, implement your changes and submit a pull request.
### License
MIT