pub struct Balancer {
targets: Vec<Target>,
strategy: Strategy,
counter: AtomicUsize,
in_flight: Vec<AtomicUsize>,
}Expand description
A pool of backend targets plus the state driving target selection.
Fields§
§targets: Vec<Target>§strategy: Strategy§counter: AtomicUsizeMonotonic pick counter driving round-robin selection.
in_flight: Vec<AtomicUsize>Per-target in-flight counts (parallel to targets), used by
least-connections selection and kept current via the guards.
Implementations§
Source§impl Balancer
impl Balancer
Sourcepub fn new(targets: Vec<Target>, strategy: Strategy) -> Result<Self, String>
pub fn new(targets: Vec<Target>, strategy: Strategy) -> Result<Self, String>
Builds a balancer over targets. Errors if the pool is empty.
Sourcepub fn select(&self, remote_addr: &str) -> usize
pub fn select(&self, remote_addr: &str) -> usize
Picks the index of the target to use; remote_addr (client ip:port)
is only consulted for ip_hash.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Whether the pool is empty (always false for a constructed Balancer).
Sourcepub fn in_flight_count(&self, idx: usize) -> usize
pub fn in_flight_count(&self, idx: usize) -> usize
Current in-flight count for a target (for tests and future metrics).
Sourcepub fn acquire(&self, idx: usize) -> ConnGuard<'_>
pub fn acquire(&self, idx: usize) -> ConnGuard<'_>
Increments the in-flight count for idx, returning a borrowed guard that
decrements it on drop. For a request/connection whose lifetime is a
single stack scope (HTTP request, TCP connection).
Sourcepub fn owned_acquire(self: &Arc<Self>, idx: usize) -> OwnedConnGuard
pub fn owned_acquire(self: &Arc<Self>, idx: usize) -> OwnedConnGuard
Like acquire but owns an Arc<Balancer>, so the guard
can be stored past the borrowing scope (e.g. a UDP session in a map).
Auto Trait Implementations§
impl !Freeze for Balancer
impl RefUnwindSafe for Balancer
impl Send for Balancer
impl Sync for Balancer
impl Unpin for Balancer
impl UnsafeUnpin for Balancer
impl UnwindSafe for Balancer
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more