Rust

group

Module

[Group]: balancers tried in order until one chooses an option.

Contents

use utopia_balancer::Group;
Rust
pub struct Group<T, A>

Balancers tried in the order they were added: the first that chooses an option wins. Use it for fallbacks, such as preferring healthy and idle workers, then any healthy one, then anything.

Every Group::run records its duration in seconds on the balancer.run.duration histogram, with the group's name as the name attribute.

Rust
use utopia_balancer::{Balancer, Candidate, First, Group};

let options = [
    Candidate::from([("dc", "fra-1".into()), ("online", false.into())]),
    Candidate::from([("dc", "lon-1".into()), ("online", true.into())]),
];

let mut online = Balancer::new(First);
online.add_filter(|c: &Candidate| c.get("online").is_some_and(|v| v.truthy()));
let mut any = Balancer::new(First);
for option in &options {
    online.add_option(option.clone());
    any.add_option(option.clone());
}

let mut group = Group::new(Some("workers"));
group.add(online).add(any);
assert_eq!(group.run()?.and_then(|c| c.get("dc")), Some(&"lon-1".into()));
Also implements Debug.