utopia_balancer/lib.rs
1//! Choosing between endpoints: keep a list of options (workers, regions,
2//! upstream servers), filter out the ones that cannot serve right now, and let
3//! an algorithm choose one of the rest.
4//!
5//! A [`Balancer`] holds options of any type, [filters](Balancer::add_filter)
6//! and an [`Algorithm`]: [`First`], [`Last`], [`Random`] or [`RoundRobin`], or
7//! your own. [`Candidate`] is a ready-made option: a bag of named values such as
8//! `hostname`, `online` or `cpu`.
9//!
10//! ```
11//! use utopia_balancer::{Balancer, Candidate, First};
12//!
13//! let mut balancer = Balancer::new(First);
14//! balancer
15//! .add_option(Candidate::from([("dc", "fra-1".into()), ("cpu", 91.into())]))
16//! .add_option(Candidate::from([("dc", "lon-1".into()), ("cpu", 35.into())]))
17//! .add_filter(|c| c.get("cpu").is_some_and(|cpu| cpu.to_double() < 50.0));
18//!
19//! assert_eq!(balancer.run()?.and_then(|c| c.get("dc")), Some(&"lon-1".into()));
20//! # Ok::<(), utopia_balancer::Error>(())
21//! ```
22//!
23//! ```php
24//! use Utopia\Balancer\Algorithm\First;
25//! use Utopia\Balancer\Balancer;
26//! use Utopia\Balancer\Option;
27//!
28//! $balancer = new Balancer(new First());
29//! $balancer
30//! ->addOption(new Option(['dc' => 'fra-1', 'cpu' => 91]))
31//! ->addOption(new Option(['dc' => 'lon-1', 'cpu' => 35]))
32//! ->addFilter(fn (Option $o) => $o->getState('cpu') < 50);
33//!
34//! echo $balancer->run()->getState('dc'), "\n"; // lon-1
35//! ```
36//!
37//! PHP: ports `utopia-php/balancer`.
38//!
39//! | PHP | Rust |
40//! |---|---|
41//! | `Utopia\Balancer\Balancer` | [`Balancer`] (generic over the option type) |
42//! | `Balancer::run()`, `getFilteredOptions()`, `getOptions()`, `getAlgo()` | [`Balancer::run`] (and [`Balancer::pick`] for the position), [`Balancer::filtered`], [`Balancer::options`], [`Balancer::algorithm`] |
43//! | `Balancer::addOption()`, `addFilter()` | [`Balancer::add_option`], [`Balancer::add_filter`] |
44//! | `Utopia\Balancer\Algorithm` | [`Algorithm`] (`run()` is [`Algorithm::pick`], returning a position) |
45//! | `Algorithm\First`, `Last`, `Random`, `RoundRobin` | [`First`], [`Last`], [`Random`], [`RoundRobin`] |
46//! | `RoundRobin::getIndex()`, `setIndex()` | [`RoundRobin::index`], [`RoundRobin::set_index`] |
47//! | `Utopia\Balancer\Option` | [`Candidate`] |
48//! | `Option::getState()`, `setState()`, `deleteState()`, `getStates()` | [`Candidate::get`] / [`Candidate::get_or`], [`Candidate::set`], [`Candidate::remove`], [`Candidate::states`] |
49//! | `\ValueError`, `\TypeError` | [`Error::Value`], [`Error::Type`] |
50//!
51//! `Utopia\Balancer\Group` has no counterpart here yet: it records to
52//! `utopia-telemetry`, which is not in this repository yet.
53//!
54//! PHP shares `Option` objects between balancers by reference, so state set
55//! on one is seen by all. In Rust, share a `Arc<Mutex<Candidate>>` (or any
56//! shared type) as the option type for the same effect, or clone values when
57//! every balancer may hold its own copy.
58//!
59//! PHP's `Random` draws from the process-wide engine that `mt_srand()` seeds;
60//! each [`Random`] here owns its generator, and [`Random::seeded`] gives the
61//! sequence PHP draws after `mt_srand($seed)`.
62
63#![warn(missing_docs)]
64
65mod algorithm;
66mod balancer;
67mod candidate;
68mod error;
69
70pub use algorithm::{Algorithm, First, Last, Random, RoundRobin};
71pub use balancer::{Balancer, Filter};
72pub use candidate::Candidate;
73pub use error::Error;
74
75/// The getting started guide (`guide.md`), compiled and run with the doctests.
76#[cfg(doctest)]
77#[doc = include_str!("../guide.md")]
78pub struct GettingStarted;