pub struct ConsistentHash { /* private fields */ }Expand description
Consistent hashing with virtual nodes.
Routes requests with the same hash_key to the same node,
and minimizes remapping when nodes are added or removed.
The hash ring is cached and only rebuilt when the candidate set changes.
Requires SelectionContext::hash_key to be set for meaningful routing.
When hash_key is None, defaults to 0 — all unkeyed requests route
to the same node.
§Examples
use loadwise::{Node, Strategy, SelectionContext};
use loadwise::strategy::ConsistentHash;
struct Backend(String);
impl Node for Backend {
type Id = String;
fn id(&self) -> &String { &self.0 }
}
let ch = ConsistentHash::default(); // 150 virtual nodes per real node
let nodes = [Backend("a".into()), Backend("b".into()), Backend("c".into())];
// Same hash_key always maps to the same node.
let ctx = SelectionContext::builder().hash_key(42).build();
let first = ch.select(&nodes, &ctx);
let second = ch.select(&nodes, &ctx);
assert_eq!(first, second);Implementations§
Source§impl ConsistentHash
impl ConsistentHash
Sourcepub fn builder() -> ConsistentHashBuilder
pub fn builder() -> ConsistentHashBuilder
Create an instance of ConsistentHash using the builder syntax
Trait Implementations§
Source§impl Debug for ConsistentHash
impl Debug for ConsistentHash
Source§impl Default for ConsistentHash
impl Default for ConsistentHash
Source§impl<N: Node> Strategy<N> for ConsistentHash
impl<N: Node> Strategy<N> for ConsistentHash
Source§fn select(&self, candidates: &[N], ctx: &SelectionContext) -> Option<usize>
fn select(&self, candidates: &[N], ctx: &SelectionContext) -> Option<usize>
Pick the next node. Returns
None when candidates is empty or all
candidates are excluded via SelectionContext::exclude.Auto Trait Implementations§
impl !Freeze for ConsistentHash
impl RefUnwindSafe for ConsistentHash
impl Send for ConsistentHash
impl Sync for ConsistentHash
impl Unpin for ConsistentHash
impl UnsafeUnpin for ConsistentHash
impl UnwindSafe for ConsistentHash
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more