pub struct AdaptiveLoadBalancer {
pub algorithm: LbAlgorithm,
pub peers: Vec<LbPeer>,
pub rr_index: usize,
pub virtual_nodes: usize,
pub total_requests: u64,
pub total_errors: u64,
/* private fields */
}Expand description
Production-grade, multi-algorithm load balancer with dynamic weight adjustment and consistent-hash support.
Fields§
§algorithm: LbAlgorithmActive routing algorithm.
peers: Vec<LbPeer>Ordered list of registered peers (insertion order is preserved).
rr_index: usizeRound-robin cursor (index into peers after filtering healthy ones).
virtual_nodes: usizeNumber of virtual nodes per peer in the consistent-hash ring.
total_requests: u64Total requests successfully routed by this balancer instance.
total_errors: u64Total errors recorded via record_completion.
Implementations§
Source§impl AdaptiveLoadBalancer
impl AdaptiveLoadBalancer
Sourcepub fn new(algorithm: LbAlgorithm) -> Self
pub fn new(algorithm: LbAlgorithm) -> Self
Creates a new, empty balancer using the given algorithm.
virtual_nodes defaults to 150.
Sourcepub fn add_peer(&mut self, peer: LbPeer)
pub fn add_peer(&mut self, peer: LbPeer)
Register a peer. If a peer with the same id already exists it is replaced with the new record.
Sourcepub fn remove_peer(&mut self, peer_id: &str) -> bool
pub fn remove_peer(&mut self, peer_id: &str) -> bool
Remove a peer by id. Returns true if the peer was found and removed.
Sourcepub fn select(&mut self, request: &LbRequest, now: u64) -> Option<LbDecision>
pub fn select(&mut self, request: &LbRequest, now: u64) -> Option<LbDecision>
Route a request to the best available peer.
Returns None when no healthy peers are available.
Sourcepub fn record_connection_start(&mut self, peer_id: &str) -> bool
pub fn record_connection_start(&mut self, peer_id: &str) -> bool
Increment active_connections for a peer. Returns false if unknown.
Sourcepub fn record_completion(
&mut self,
peer_id: &str,
latency_ms: f64,
success: bool,
_now: u64,
) -> bool
pub fn record_completion( &mut self, peer_id: &str, latency_ms: f64, success: bool, _now: u64, ) -> bool
Record completion of a request.
- Decrements
active_connections(saturating at 0). - Updates
avg_latency_mswith EMA alpha = 0.2. - Increments
total_errors(per-peer and global) on failure.
Returns false when the peer is not registered.
Sourcepub fn mark_healthy(&mut self, peer_id: &str, healthy: bool) -> bool
pub fn mark_healthy(&mut self, peer_id: &str, healthy: bool) -> bool
Set a peer’s healthy flag. Returns false if the peer is unknown.
Sourcepub fn adjust_weights_by_latency(&mut self)
pub fn adjust_weights_by_latency(&mut self)
Auto-tune weights based on current latency estimates.
Formula: weight[i] = max(1, round(1000.0 / avg_latency_ms))
Peers with avg_latency_ms == 0 are skipped (latency not yet measured).
Sourcepub fn healthy_peers(&self) -> Vec<&LbPeer>
pub fn healthy_peers(&self) -> Vec<&LbPeer>
Returns references to all healthy peers.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Total number of registered peers.
Sourcepub fn healthy_peer_count(&self) -> usize
pub fn healthy_peer_count(&self) -> usize
Number of healthy peers.
Sourcepub fn stats(&self) -> AdaptiveLbStats
pub fn stats(&self) -> AdaptiveLbStats
Aggregate statistics snapshot.
Auto Trait Implementations§
impl Freeze for AdaptiveLoadBalancer
impl RefUnwindSafe for AdaptiveLoadBalancer
impl Send for AdaptiveLoadBalancer
impl Sync for AdaptiveLoadBalancer
impl Unpin for AdaptiveLoadBalancer
impl UnsafeUnpin for AdaptiveLoadBalancer
impl UnwindSafe for AdaptiveLoadBalancer
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