pub struct PeerLoadBalancer { /* private fields */ }Expand description
Peer-aware load balancer.
Distributes requests across known peers using a configurable PlbStrategy.
Tracks health, latency, and in-flight requests per peer and recomputes
normalised weights after every completed request.
Implementations§
Source§impl PeerLoadBalancer
impl PeerLoadBalancer
Sourcepub fn new(config: PlbBalancerConfig) -> Self
pub fn new(config: PlbBalancerConfig) -> Self
Create a new balancer with the given configuration.
Sourcepub fn default_config() -> Self
pub fn default_config() -> Self
Create a balancer with default configuration.
Sourcepub fn add_peer(&mut self, id: PlbPeerId, capacity: u32)
pub fn add_peer(&mut self, id: PlbPeerId, capacity: u32)
Register a new peer with the given capacity. If the peer already exists its capacity is updated.
Sourcepub fn remove_peer(&mut self, id: &PlbPeerId) -> Option<PlbPeerState>
pub fn remove_peer(&mut self, id: &PlbPeerId) -> Option<PlbPeerState>
Remove a peer from the balancer.
Sourcepub fn set_healthy(
&mut self,
id: &PlbPeerId,
healthy: bool,
) -> Result<(), PlbError>
pub fn set_healthy( &mut self, id: &PlbPeerId, healthy: bool, ) -> Result<(), PlbError>
Manually override the health state of a peer.
Sourcepub fn cooldown_peer(&mut self, id: &PlbPeerId) -> Result<(), PlbError>
pub fn cooldown_peer(&mut self, id: &PlbPeerId) -> Result<(), PlbError>
Put a peer in cooldown for config.cooldown_ms milliseconds.
Sourcepub fn select_peer(&mut self, key: &[u8]) -> Result<PlbPeerId, PlbError>
pub fn select_peer(&mut self, key: &[u8]) -> Result<PlbPeerId, PlbError>
Select a peer for the next request.
key is used only by PlbStrategy::ConsistentHash; other strategies ignore it.
Sourcepub fn record_result(
&mut self,
peer_id: &PlbPeerId,
latency_ms: f64,
success: bool,
) -> Result<(), PlbError>
pub fn record_result( &mut self, peer_id: &PlbPeerId, latency_ms: f64, success: bool, ) -> Result<(), PlbError>
Record the outcome of a completed request.
Updates EWMA latency, success/failure counters and recomputes weights.
Sourcepub fn recompute_weights(&mut self)
pub fn recompute_weights(&mut self)
Recompute normalised weights for all peers.
Weight formula per peer:
w = capacity_score × latency_score × health_score
where:
capacity_score = (capacity - in_flight) / capacity(0 if zero capacity)latency_score = 1 / (1 + avg_latency_ms / 1000)health_score = success_rate(1.0 for new peers with no history)
Sourcepub fn run_health_check(&mut self)
pub fn run_health_check(&mut self)
Evaluate all peers and mark unhealthy those whose failure rate exceeds
1 - health_threshold.
Sourcepub fn balancer_stats(&self) -> PlbBalancerStats
pub fn balancer_stats(&self) -> PlbBalancerStats
Return an aggregate snapshot of balancer statistics.
Sourcepub fn config(&self) -> &PlbBalancerConfig
pub fn config(&self) -> &PlbBalancerConfig
Return the current configuration.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Return the number of registered peers.
Sourcepub fn peer_state(&self, id: &PlbPeerId) -> Option<&PlbPeerState>
pub fn peer_state(&self, id: &PlbPeerId) -> Option<&PlbPeerState>
Retrieve state for a specific peer.
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