Expand description
Choosing between endpoints: keep a list of options (workers, regions, upstream servers), filter out the ones that cannot serve right now, and let an algorithm choose one of the rest.
A Balancer holds options of any type, filters
and an Algorithm: First, Last, Random or RoundRobin, or
your own. Candidate is a ready-made option: a bag of named values such as
hostname, online or cpu.
use utopia_balancer::{Balancer, Candidate, First};
let mut balancer = Balancer::new(First);
balancer
.add_option(Candidate::from([("dc", "fra-1".into()), ("cpu", 91.into())]))
.add_option(Candidate::from([("dc", "lon-1".into()), ("cpu", 35.into())]))
.add_filter(|c| c.get("cpu").is_some_and(|cpu| cpu.to_double() < 50.0));
assert_eq!(balancer.run()?.and_then(|c| c.get("dc")), Some(&"lon-1".into()));use Utopia\Balancer\Algorithm\First;
use Utopia\Balancer\Balancer;
use Utopia\Balancer\Option;
$balancer = new Balancer(new First());
$balancer
->addOption(new Option(['dc' => 'fra-1', 'cpu' => 91]))
->addOption(new Option(['dc' => 'lon-1', 'cpu' => 35]))
->addFilter(fn (Option $o) => $o->getState('cpu') < 50);
echo $balancer->run()->getState('dc'), "\n"; // lon-1PHP: ports utopia-php/balancer.
| PHP | Rust |
|---|---|
Utopia\Balancer\Balancer | Balancer (generic over the option type) |
Balancer::run(), getFilteredOptions(), getOptions(), getAlgo() | Balancer::run (and Balancer::pick for the position), Balancer::filtered, Balancer::options, Balancer::algorithm |
Balancer::addOption(), addFilter() | Balancer::add_option, Balancer::add_filter |
Utopia\Balancer\Algorithm | Algorithm (run() is Algorithm::pick, returning a position) |
Algorithm\First, Last, Random, RoundRobin | First, Last, Random, RoundRobin |
RoundRobin::getIndex(), setIndex() | RoundRobin::index, RoundRobin::set_index |
Utopia\Balancer\Option | Candidate |
Option::getState(), setState(), deleteState(), getStates() | Candidate::get / Candidate::get_or, Candidate::set, Candidate::remove, Candidate::states |
\ValueError, \TypeError | Error::Value, Error::Type |
Utopia\Balancer\Group has no counterpart here yet: it records to
utopia-telemetry, which is not in this repository yet.
PHP shares Option objects between balancers by reference, so state set
on one is seen by all. In Rust, share a Arc<Mutex<Candidate>> (or any
shared type) as the option type for the same effect, or clone values when
every balancer may hold its own copy.
PHP’s Random draws from the process-wide engine that mt_srand() seeds;
each Random here owns its generator, and Random::seeded gives the
sequence PHP draws after mt_srand($seed).
Structs§
- Balancer
- Chooses one of its options with an
Algorithm, among those every filter accepts. - Candidate
- One option to balance between, described by a bag of named values (its state): a hostname, whether it is online, its load.
- First
- Always the first option.
- Last
- Always the last option.
- Random
- A uniformly random option, from PHP’s Mersenne Twister.
- Round
Robin - Each option in turn: every pick moves to the next position and wraps to the first after the last.
Enums§
- Error
- Why choosing an option failed.
Traits§
- Algorithm
- Chooses one of the options that passed a balancer’s filters.
Type Aliases§
- Filter
- A filter: whether an option may be chosen.