Created
January 12, 2018 17:47
-
-
Save cholcombe973/a39af08dbc4eaad0a12581b19920c72f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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