Skip to main content

Balancer

Struct Balancer 

Source
pub struct Balancer<T, A> { /* private fields */ }
Expand description

Chooses one of its options with an Algorithm, among those every filter accepts.

Options are any type: Candidate for PHP’s state bags, or your own endpoint type. Filters run on every Balancer::run, so they see the options’ current state.

PHP: Utopia\Balancer\Balancer.

use utopia_balancer::{Balancer, Candidate, RoundRobin};

let mut balancer = Balancer::new(RoundRobin::new(-1));
balancer
    .add_option(Candidate::from([("hostname", "worker-1".into()), ("online", true.into())]))
    .add_option(Candidate::from([("hostname", "worker-2".into()), ("online", false.into())]))
    .add_option(Candidate::from([("hostname", "worker-3".into()), ("online", true.into())]))
    .add_filter(|c| c.get("online").is_some_and(|v| v.truthy()));

let host = |b: &mut Balancer<Candidate, RoundRobin>| b.run().unwrap().and_then(|c| c.get("hostname").cloned());
assert_eq!(host(&mut balancer), Some("worker-1".into()));
assert_eq!(host(&mut balancer), Some("worker-3".into()));
assert_eq!(host(&mut balancer), Some("worker-1".into()));
use Utopia\Balancer\Algorithm\RoundRobin;
use Utopia\Balancer\Balancer;
use Utopia\Balancer\Option;

$balancer = new Balancer(new RoundRobin(-1));
$balancer
    ->addOption(new Option(['hostname' => 'worker-1', 'online' => true]))
    ->addOption(new Option(['hostname' => 'worker-2', 'online' => false]))
    ->addOption(new Option(['hostname' => 'worker-3', 'online' => true]))
    ->addFilter(fn (Option $o) => $o->getState('online') === true);

echo $balancer->run()->getState('hostname'), "\n"; // worker-1
echo $balancer->run()->getState('hostname'), "\n"; // worker-3
echo $balancer->run()->getState('hostname'), "\n"; // worker-1

Implementations§

Source§

impl<T, A: Algorithm<T>> Balancer<T, A>

Source

pub fn new(algorithm: A) -> Self

A balancer without options, choosing with algorithm.

PHP: new Balancer($algo).

Source

pub fn algorithm(&self) -> &A

The algorithm.

PHP: getAlgo().

Source

pub fn algorithm_mut(&mut self) -> &mut A

The algorithm, to change its state (such as a RoundRobin index).

PHP: getAlgo(), which returns the object itself.

Source

pub fn add_option(&mut self, option: T) -> &mut Self

Adds an option after the others.

PHP: addOption($option).

Source

pub fn options(&self) -> &[T]

Every option, in the order they were added, filtered or not.

PHP: getOptions().

Source

pub fn add_filter( &mut self, filter: impl Fn(&T) -> bool + Send + Sync + 'static, ) -> &mut Self

Adds a filter: only options every filter accepts can be chosen.

PHP: addFilter($callable).

Source

pub fn filtered(&self) -> Vec<&T>

Every option every filter accepts, in the order they were added: what the algorithm chooses from, for callers that act on all of them.

PHP: getFilteredOptions().

Source

pub fn pick(&mut self) -> Result<Option<usize>, Error>

The position in Balancer::options of the option the algorithm chooses among the filtered ones, or None when none passes or the algorithm chooses nothing.

PHP: run(), as a position.

Source

pub fn run(&mut self) -> Result<Option<&T>, Error>

The option the algorithm chooses among the filtered ones, or None when none passes or the algorithm chooses nothing. The algorithm is not consulted without options.

PHP: run().

Trait Implementations§

Source§

impl<T: Clone, A: Clone> Clone for Balancer<T, A>

Source§

fn clone(&self) -> Self

A copy with cloned options and algorithm, sharing the filters.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, A: Algorithm<T>> Debug for Balancer<T, A>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

The algorithm’s name and the number of options and filters.

Auto Trait Implementations§

§

impl<T, A> !RefUnwindSafe for Balancer<T, A>

§

impl<T, A> !UnwindSafe for Balancer<T, A>

§

impl<T, A> Forget for Balancer<T, A>

§

impl<T, A> Freeze for Balancer<T, A>
where A: Freeze, Vec<T>: Freeze, Vec<Arc<dyn Fn(&T) -> bool + Sync + Send>>: Freeze,

§

impl<T, A> Move for Balancer<T, A>

§

impl<T, A> Send for Balancer<T, A>
where A: Send, Vec<T>: Send, Vec<Arc<dyn Fn(&T) -> bool + Sync + Send>>: Send,

§

impl<T, A> Sync for Balancer<T, A>
where A: Sync, Vec<T>: Sync, Vec<Arc<dyn Fn(&T) -> bool + Sync + Send>>: Sync,

§

impl<T, A> Unpin for Balancer<T, A>
where A: Unpin, Vec<T>: Unpin, Vec<Arc<dyn Fn(&T) -> bool + Sync + Send>>: Unpin,

§

impl<T, A> UnsafeUnpin for Balancer<T, A>
where A: UnsafeUnpin, Vec<T>: UnsafeUnpin, Vec<Arc<dyn Fn(&T) -> bool + Sync + Send>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.