Skip to main content

Strategy

Trait Strategy 

Source
pub trait Strategy<N>: Send + Sync {
    // Required method
    fn select(&self, candidates: &[N], ctx: &SelectionContext) -> Option<usize>;
}
Expand description

A load balancing strategy that selects a node from a list of candidates.

Returns the index of the selected node within the candidates slice. Implementations must use interior mutability for any state (e.g., AtomicUsize).

§Examples

use loadwise::{Strategy, SelectionContext};
use loadwise::strategy::RoundRobin;

let rr = RoundRobin::new();
let nodes = ["a", "b", "c"];
let ctx = SelectionContext::default();

assert_eq!(rr.select(&nodes, &ctx), Some(0));
assert_eq!(rr.select(&nodes, &ctx), Some(1));
assert_eq!(rr.select(&nodes, &ctx), Some(2));
assert_eq!(rr.select(&nodes, &ctx), Some(0)); // wraps around

Required Methods§

Source

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.

Implementors§