Skip to content

Instantly share code, notes, and snippets.

@cholcombe973
Created January 12, 2018 17:47
Show Gist options
  • Save cholcombe973/a39af08dbc4eaad0a12581b19920c72f to your computer and use it in GitHub Desktop.
Save cholcombe973/a39af08dbc4eaad0a12581b19920c72f to your computer and use it in GitHub Desktop.
extern crate conhash;
#[macro_use]
extern crate log;
extern crate simplelog;
use conhash::{ConsistentHash, Node};
use simplelog::{Config, TermLogger};
#[derive(Debug, Clone, Eq, PartialEq)]
struct ServerNode {
host: String,
port: u16,
}
impl Node for ServerNode {
fn name(&self) -> String {
format!("{}:{}", self.host, self.port)
}
}
impl ServerNode {
fn new(host: &str, port: u16) -> ServerNode {
ServerNode {
host: host.to_owned(),
port: port,
}
}
}
fn main() {
TermLogger::init(simplelog::LogLevelFilter::Debug, Config::default()).unwrap();
let nodes = [
ServerNode::new("localhost", 12345),
ServerNode::new("localhost", 12346),
ServerNode::new("localhost", 12347),
];
const REPLICAS: usize = 3;
let mut ch = ConsistentHash::new();
for node in nodes.iter() {
ch.add(node, REPLICAS);
}
assert_eq!(ch.len(), nodes.len() * REPLICAS);
let node_for_hello = ch.get(&"hello3".as_bytes()).unwrap().clone();
println!("node_for_hello: {:?}", node_for_hello);
println!("Removing 12347");
ch.remove(&ServerNode::new("localhost", 12347));
let node_for_hello = ch.get(&"hello3".as_bytes()).unwrap().clone();
println!("node_for_hello: {:?}", node_for_hello);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment