pub struct PeerLoadBalancer {
pub peers: HashMap<String, PeerLoad>,
pub strategy: LbStrategy,
pub total_requests: u64,
}Expand description
Distributes requests across peers using a configurable LbStrategy.
Fields§
§peers: HashMap<String, PeerLoad>All registered peers, keyed by peer id.
strategy: LbStrategyActive load-balancing strategy.
total_requests: u64Monotonically increasing counter; drives round-robin index and
weighted-random index. Incremented on every successful select().
Implementations§
Source§impl PeerLoadBalancer
impl PeerLoadBalancer
Sourcepub fn new(strategy: LbStrategy) -> Self
pub fn new(strategy: LbStrategy) -> Self
Creates a new, empty load balancer using strategy.
Sourcepub fn add_peer(&mut self, peer_id: &str, weight: u32)
pub fn add_peer(&mut self, peer_id: &str, weight: u32)
Registers a peer. If the peer already exists its weight is updated.
Sourcepub fn remove_peer(&mut self, peer_id: &str) -> bool
pub fn remove_peer(&mut self, peer_id: &str) -> bool
Removes a peer. Returns true when the peer existed.
Sourcepub fn select(&mut self) -> Option<String>
pub fn select(&mut self) -> Option<String>
Selects a peer according to the configured strategy.
Only healthy peers are considered. Returns None when no healthy
peers are available. On success the internal request counter is
incremented.
Sourcepub fn record_start(&mut self, peer_id: &str) -> bool
pub fn record_start(&mut self, peer_id: &str) -> bool
Marks the start of a request to peer_id.
Increments active_connections and total_requests on the peer.
Returns false when the peer is not registered.
Sourcepub fn record_complete(
&mut self,
peer_id: &str,
latency_ms: f64,
success: bool,
) -> bool
pub fn record_complete( &mut self, peer_id: &str, latency_ms: f64, success: bool, ) -> bool
Records the completion of a request to peer_id.
- Decrements
active_connections(saturating at 0). - Updates
avg_latency_mswith EWMA alpha = 0.2. - Increments
failed_requestswhensuccessisfalse.
Returns false when the peer is not registered.
Auto Trait Implementations§
impl Freeze for PeerLoadBalancer
impl RefUnwindSafe for PeerLoadBalancer
impl Send for PeerLoadBalancer
impl Sync for PeerLoadBalancer
impl Unpin for PeerLoadBalancer
impl UnsafeUnpin for PeerLoadBalancer
impl UnwindSafe for PeerLoadBalancer
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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