pub struct RoundRobin { /* private fields */ }Expand description
Each option in turn: every pick moves to the next position and wraps to the first after the last.
The index is the position picked last. It starts where it is created
(-1 starts at the first option) and can be moved with
RoundRobin::set_index, to share a rotation across processes.
PHP: Utopia\Balancer\Algorithm\RoundRobin.
use utopia_balancer::{Algorithm, RoundRobin};
let options = ["fra-1", "fra-2", "lon-1"];
let refs: Vec<&&str> = options.iter().collect();
let mut rr = RoundRobin::new(-1);
let picks: Vec<_> = (0..4).map(|_| rr.pick(&refs).unwrap().unwrap()).collect();
assert_eq!(picks, [0, 1, 2, 0]);
assert_eq!(rr.index(), 0);use Utopia\Balancer\Algorithm\RoundRobin;
use Utopia\Balancer\Option;
$options = [new Option(['dc' => 'fra-1']), new Option(['dc' => 'fra-2']), new Option(['dc' => 'lon-1'])];
$rr = new RoundRobin(-1);
foreach ([1, 2, 3, 4] as $_) {
echo $rr->run($options)->getState('dc'), "\n"; // fra-1, fra-2, lon-1, fra-1
}
echo $rr->getIndex(), "\n"; // 0Implementations§
Trait Implementations§
Source§impl<T: ?Sized> Algorithm<T> for RoundRobin
impl<T: ?Sized> Algorithm<T> for RoundRobin
Source§fn pick(&mut self, options: &[&T]) -> Result<Option<usize>, Error>
fn pick(&mut self, options: &[&T]) -> Result<Option<usize>, Error>
The position after the last one, or the first when that is past the
end (or before the start). Without options the index resets to 0
and nothing is picked.
PHP: an index at PHP_INT_MAX cannot be incremented (a TypeError
for the typed property); that is Error::Type here, with the index
left as it was.
Source§impl Clone for RoundRobin
impl Clone for RoundRobin
impl Copy for RoundRobin
Source§impl Debug for RoundRobin
impl Debug for RoundRobin
impl Eq for RoundRobin
Source§impl PartialEq for RoundRobin
impl PartialEq for RoundRobin
impl StructuralPartialEq for RoundRobin
Auto Trait Implementations§
impl Forget for RoundRobin
impl Freeze for RoundRobin
impl Move for RoundRobin
impl RefUnwindSafe for RoundRobin
impl Send for RoundRobin
impl Sync for RoundRobin
impl Unpin for RoundRobin
impl UnsafeUnpin for RoundRobin
impl UnwindSafe for RoundRobin
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.