pub struct LoadBalancer { /* private fields */ }Expand description
Thread-safe weighted round-robin load balancer for model endpoints.
§Example
use tokio_prompt_orchestrator::load_balancer::{
BalancerConfig, LoadBalancer, ModelEndpoint,
};
use std::time::Duration;
let config = BalancerConfig {
endpoints: vec![
ModelEndpoint {
id: "ep-a".to_string(),
url: "http://a".to_string(),
weight: 2,
max_rps: 100.0,
healthy: true,
latency_p99_ms: 0.0,
},
ModelEndpoint {
id: "ep-b".to_string(),
url: "http://b".to_string(),
weight: 1,
max_rps: 50.0,
healthy: true,
latency_p99_ms: 0.0,
},
],
health_check_interval: Duration::from_secs(30),
failover: true,
};
let lb = LoadBalancer::new(config);
let ep = lb.select();
assert!(ep.is_some());Implementations§
Source§impl LoadBalancer
impl LoadBalancer
Sourcepub fn new(config: BalancerConfig) -> Self
pub fn new(config: BalancerConfig) -> Self
Create a new load balancer from the given configuration.
Sourcepub fn select(&self) -> Option<ModelEndpoint>
pub fn select(&self) -> Option<ModelEndpoint>
Select the next endpoint using weighted round-robin.
Returns None when:
- No endpoints are configured.
- All endpoints are unhealthy and
failover = true.
Sourcepub fn mark_success(&self, id: &str, latency_ms: f64)
pub fn mark_success(&self, id: &str, latency_ms: f64)
Record a successful response from endpoint id with the given latency.
Resets the consecutive-failure counter and marks the endpoint healthy.
Sourcepub fn mark_failure(&self, id: &str)
pub fn mark_failure(&self, id: &str)
Record a failure from endpoint id.
After 3 consecutive failures the endpoint is marked unhealthy.
Sourcepub fn stats(&self) -> LoadBalancerStats
pub fn stats(&self) -> LoadBalancerStats
Return a snapshot of current balancer statistics.
Sourcepub fn failover_enabled(&self) -> bool
pub fn failover_enabled(&self) -> bool
Return the configured failover setting.
Trait Implementations§
Source§impl Clone for LoadBalancer
impl Clone for LoadBalancer
Source§fn clone(&self) -> LoadBalancer
fn clone(&self) -> LoadBalancer
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for LoadBalancer
impl RefUnwindSafe for LoadBalancer
impl Send for LoadBalancer
impl Sync for LoadBalancer
impl Unpin for LoadBalancer
impl UnsafeUnpin for LoadBalancer
impl UnwindSafe for LoadBalancer
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request