Balancer
RustIn Appwrite#
Appwrite runs the Rust crate: the PHP extension serves the library and none of its PHP is left.
Rust 704 lines (258 code)· PHP 0 lines (0 code)split
| Part | Code | Comments | Blank | Total | Files |
|---|---|---|---|---|---|
| Rust source | 258 | 381 | 65 | 704 | 5 |
| PHP source | 0 | 0 | 0 | 0 | 0 |
Comments include doc comments. Each side counts its src/; the PHP side is what the package still has in PHP.
Served from Rust (8)
Utopia\Balancer\AlgorithmUtopia\Balancer\Algorithm\FirstUtopia\Balancer\Algorithm\LastUtopia\Balancer\Algorithm\RandomUtopia\Balancer\Algorithm\RoundRobinUtopia\Balancer\BalancerUtopia\Balancer\GroupUtopia\Balancer\OptionInstall#
cargo add utopia-balancerGetting started#
Common tasks with Balancer, each a complete program you can copy and run.
A balancer picks one endpoint out of many: a worker for the next job, a region for the next request. You give it options, filters that rule out the options that cannot serve right now, and an algorithm that chooses among the rest. Options can be any type; Candidate is a ready-made one holding named values such as a hostname or a CPU load. This guide covers the common tasks.
Rotate between options
RoundRobin hands out each option in turn and starts over after the last. Start it at -1 to begin with the first option.
use utopia_balancer::{Balancer, Candidate, RoundRobin};
let mut balancer = Balancer::new(RoundRobin::new(-1));
for host in ["worker-1", "worker-2", "worker-3"] {
balancer.add_option(Candidate::from([("hostname", host.into())]));
}
let mut picks = Vec::new();
for _ in 0..4 {
let option = balancer.run()?.expect("an option");
picks.push(option.get("hostname").cloned());
}
assert_eq!(picks, [Some("worker-1".into()), Some("worker-2".into()), Some("worker-3".into()), Some("worker-1".into())]);use Utopia\Balancer\Algorithm\RoundRobin;
use Utopia\Balancer\Balancer;
use Utopia\Balancer\Option;
$balancer = new Balancer(new RoundRobin(-1));
foreach (['worker-1', 'worker-2', 'worker-3'] as $host) {
$balancer->addOption(new Option(['hostname' => $host]));
}
foreach ([1, 2, 3, 4] as $_) {
echo $balancer->run()->getState('hostname'), "\n"; // worker-1, worker-2, worker-3, worker-1
}Skip options that cannot serve
Filters decide which options the algorithm may choose. An option must pass every filter. filtered lists all the options that pass, for when you act on every one of them rather than choosing.
use utopia_balancer::{Balancer, Candidate, First};
let mut balancer = Balancer::new(First);
balancer
.add_option(Candidate::from([("hostname", "worker-1".into()), ("online", true.into()), ("cpu", 80.into())]))
.add_option(Candidate::from([("hostname", "worker-2".into()), ("online", false.into()), ("cpu", 20.into())]))
.add_option(Candidate::from([("hostname", "worker-3".into()), ("online", true.into()), ("cpu", 35.into())]))
.add_filter(|c| c.get("online").is_some_and(|v| v.truthy()));
assert_eq!(balancer.filtered().len(), 2);
balancer.add_filter(|c| c.get("cpu").is_some_and(|v| v.to_double() < 50.0));
assert_eq!(balancer.run()?.and_then(|c| c.get("hostname")), Some(&"worker-3".into()));use Utopia\Balancer\Algorithm\First;
use Utopia\Balancer\Balancer;
use Utopia\Balancer\Option;
$balancer = new Balancer(new First());
$balancer
->addOption(new Option(['hostname' => 'worker-1', 'online' => true, 'cpu' => 80]))
->addOption(new Option(['hostname' => 'worker-2', 'online' => false, 'cpu' => 20]))
->addOption(new Option(['hostname' => 'worker-3', 'online' => true, 'cpu' => 35]))
->addFilter(fn (Option $o) => $o->getState('online') === true);
echo \count($balancer->getFilteredOptions()), "\n"; // 2
$balancer->addFilter(fn (Option $o) => $o->getState('cpu') < 50);
echo $balancer->run()->getState('hostname'), "\n"; // worker-3Reproduce random choices
Random picks uniformly with PHP's Mersenne Twister. Seed it to replay a sequence in a test: Random::seeded(7) picks what PHP picks after mt_srand(7).
use utopia_balancer::{Balancer, Candidate, Random};
let mut balancer = Balancer::new(Random::seeded(7));
for dc in ["fra-1", "fra-2", "lon-1"] {
balancer.add_option(Candidate::from([("dc", dc.into())]));
}
let mut picks = Vec::new();
for _ in 0..3 {
picks.push(balancer.run()?.and_then(|c| c.get("dc")).cloned());
}
assert_eq!(picks, [Some("fra-1".into()), Some("fra-2".into()), Some("fra-2".into())]);use Utopia\Balancer\Algorithm\Random;
use Utopia\Balancer\Balancer;
use Utopia\Balancer\Option;
mt_srand(7);
$balancer = new Balancer(new Random());
foreach (['fra-1', 'fra-2', 'lon-1'] as $dc) {
$balancer->addOption(new Option(['dc' => $dc]));
}
foreach ([1, 2, 3] as $_) {
echo $balancer->run()->getState('dc'), "\n"; // fra-1, fra-2, fra-2
}Balance your own endpoint type
Options do not have to be Candidates. Use your own type, and filters and custom algorithms see it directly.
use utopia_balancer::{Balancer, Last};
struct Upstream {
url: &'static str,
healthy: bool,
}
let mut balancer = Balancer::new(Last);
balancer
.add_option(Upstream { url: "http://10.0.0.1", healthy: true })
.add_option(Upstream { url: "http://10.0.0.2", healthy: true })
.add_option(Upstream { url: "http://10.0.0.3", healthy: false })
.add_filter(|u| u.healthy);
assert_eq!(balancer.run()?.map(|u| u.url), Some("http://10.0.0.2"));use Utopia\Balancer\Algorithm\Last;
use Utopia\Balancer\Balancer;
use Utopia\Balancer\Option;
$balancer = new Balancer(new Last());
$balancer
->addOption(new Option(['url' => 'http://10.0.0.1', 'healthy' => true]))
->addOption(new Option(['url' => 'http://10.0.0.2', 'healthy' => true]))
->addOption(new Option(['url' => 'http://10.0.0.3', 'healthy' => false]))
->addFilter(fn (Option $o) => $o->getState('healthy'));
echo $balancer->run()->getState('url'), "\n"; // http://10.0.0.2Reference#
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.
PHP: 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).
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-1API#
What utopia_balancer exports at its root.