pub struct RiLoadBalancer { /* private fields */ }Expand description
Main load balancer implementation.
This struct provides a comprehensive load balancing solution with support for multiple strategies, health checking, connection management, and detailed statistics.
Implementations§
Source§impl RiLoadBalancer
impl RiLoadBalancer
Sourcepub fn new(strategy: RiLoadBalancerStrategy) -> Self
pub fn new(strategy: RiLoadBalancerStrategy) -> Self
Sourcepub async fn add_server(&self, server: RiBackendServer) -> RiResult<()>
pub async fn add_server(&self, server: RiBackendServer) -> RiResult<()>
Adds a backend server to the load balancer.
§Parameters
server: The backend server to add
§Security
This method validates the server URL to prevent SSRF (Server-Side Request Forgery) attacks. Only HTTP and HTTPS URLs are allowed. File://, gopher://, and other schemes are blocked. Private IP addresses (127.0.0.0/8, 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16, 169.254.0.0/16) are logged as warnings but still allowed for internal deployments.
Sourcepub async fn remove_server(&self, server_id: &str) -> bool
pub async fn remove_server(&self, server_id: &str) -> bool
Sourcepub async fn get_healthy_servers(&self) -> Vec<RiBackendServer>
pub async fn get_healthy_servers(&self) -> Vec<RiBackendServer>
Sourcepub async fn select_server(
&self,
client_ip: Option<&str>,
) -> RiResult<RiBackendServer>
pub async fn select_server( &self, client_ip: Option<&str>, ) -> RiResult<RiBackendServer>
Selects the most appropriate backend server for a client request.
This method applies the configured load balancing strategy to select a server, considering only healthy servers with available connections.
§Parameters
client_ip: Optional client IP address for IP Hash strategy
§Returns
A RiResult<RiBackendServer> with the selected server, or an error if no servers are available
Sourcepub async fn release_server(&self, server_id: &str)
pub async fn release_server(&self, server_id: &str)
Releases a server after a request is completed.
This method decrements the active connection count for the specified server.
§Parameters
server_id: ID of the server to release
Sourcepub async fn record_server_failure(&self, server_id: &str)
pub async fn record_server_failure(&self, server_id: &str)
Records a failed request to a server.
This method increments the failed request count and may mark the server as unhealthy if the failure rate exceeds a threshold.
§Parameters
server_id: ID of the server that failed
Sourcepub async fn record_response_time(&self, server_id: &str, response_time_ms: u64)
pub async fn record_response_time(&self, server_id: &str, response_time_ms: u64)
Records the response time for a successful request.
§Parameters
server_id: ID of the server that handled the requestresponse_time_ms: Response time in milliseconds
Sourcepub async fn get_server_stats(
&self,
server_id: &str,
) -> Option<RiLoadBalancerServerStats>
pub async fn get_server_stats( &self, server_id: &str, ) -> Option<RiLoadBalancerServerStats>
Sourcepub async fn get_all_stats(
&self,
) -> FxHashMap<String, RiLoadBalancerServerStats>
pub async fn get_all_stats( &self, ) -> FxHashMap<String, RiLoadBalancerServerStats>
Gets statistics for all servers.
§Returns
A FxHashMap<String, RiLoadBalancerServerStats> with statistics for all servers
Sourcepub async fn mark_server_healthy(&self, server_id: &str, healthy: bool)
pub async fn mark_server_healthy(&self, server_id: &str, healthy: bool)
Marks a server as healthy or unhealthy.
§Parameters
server_id: ID of the server to updatehealthy: New health status (true = healthy, false = unhealthy)
Sourcepub async fn perform_health_check(&self, server_id: &str) -> bool
pub async fn perform_health_check(&self, server_id: &str) -> bool
Sourcepub async fn start_health_checks(
self: Arc<Self>,
interval_secs: u64,
) -> JoinHandle<()>
pub async fn start_health_checks( self: Arc<Self>, interval_secs: u64, ) -> JoinHandle<()>
Sourcepub fn get_strategy(&self) -> &RiLoadBalancerStrategy
pub fn get_strategy(&self) -> &RiLoadBalancerStrategy
Gets the current load balancing strategy.
§Returns
A reference to the current RiLoadBalancerStrategy
Sourcepub async fn set_strategy(&mut self, strategy: RiLoadBalancerStrategy)
pub async fn set_strategy(&mut self, strategy: RiLoadBalancerStrategy)
Sourcepub async fn get_server_count(&self) -> usize
pub async fn get_server_count(&self) -> usize
Sourcepub async fn get_healthy_server_count(&self) -> usize
pub async fn get_healthy_server_count(&self) -> usize
Trait Implementations§
Source§impl Clone for RiLoadBalancer
impl Clone for RiLoadBalancer
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Creates a clone of the load balancer.
Note: The clone will have the same strategy and counter, but empty servers and stats since we can’t await in the Clone trait.
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'py> IntoPyObject<'py> for RiLoadBalancer
impl<'py> IntoPyObject<'py> for RiLoadBalancer
Source§type Target = RiLoadBalancer
type Target = RiLoadBalancer
Source§type Output = Bound<'py, <RiLoadBalancer as IntoPyObject<'py>>::Target>
type Output = Bound<'py, <RiLoadBalancer as IntoPyObject<'py>>::Target>
Source§fn into_pyobject(
self,
py: Python<'py>,
) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
fn into_pyobject( self, py: Python<'py>, ) -> Result<<Self as IntoPyObject<'_>>::Output, <Self as IntoPyObject<'_>>::Error>
Source§impl PyClass for RiLoadBalancer
impl PyClass for RiLoadBalancer
Source§impl PyClassImpl for RiLoadBalancer
impl PyClassImpl for RiLoadBalancer
Source§const IS_BASETYPE: bool = false
const IS_BASETYPE: bool = false
Source§const IS_SUBCLASS: bool = false
const IS_SUBCLASS: bool = false
Source§const IS_MAPPING: bool = false
const IS_MAPPING: bool = false
Source§const IS_SEQUENCE: bool = false
const IS_SEQUENCE: bool = false
Source§const IS_IMMUTABLE_TYPE: bool = false
const IS_IMMUTABLE_TYPE: bool = false
Source§const RAW_DOC: &'static CStr = c"Main load balancer implementation.\n\nThis struct provides a comprehensive load balancing solution with support for multiple\nstrategies, health checking, connection management, and detailed statistics.\x00"
const RAW_DOC: &'static CStr = c"Main load balancer implementation.\n\nThis struct provides a comprehensive load balancing solution with support for multiple\nstrategies, health checking, connection management, and detailed statistics.\x00"
Source§const DOC: &'static CStr
const DOC: &'static CStr
text_signature if a constructor is defined. Read moreSource§type ThreadChecker = SendablePyClass<RiLoadBalancer>
type ThreadChecker = SendablePyClass<RiLoadBalancer>
Source§type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
type PyClassMutability = <<PyAny as PyClassBaseType>::PyClassMutability as PyClassMutability>::MutableChild
Source§type BaseNativeType = PyAny
type BaseNativeType = PyAny
PyAny by default, and when you declare
#[pyclass(extends=PyDict)], it’s PyDict.fn items_iter() -> PyClassItemsIter
fn lazy_type_object() -> &'static LazyTypeObject<Self>
fn dict_offset() -> Option<isize>
fn weaklist_offset() -> Option<isize>
Source§impl PyTypeInfo for RiLoadBalancer
impl PyTypeInfo for RiLoadBalancer
Source§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object is an instance of this type.impl DerefToPyAny for RiLoadBalancer
impl ExtractPyClassWithClone for RiLoadBalancer
Auto Trait Implementations§
impl !Freeze for RiLoadBalancer
impl !RefUnwindSafe for RiLoadBalancer
impl Send for RiLoadBalancer
impl Sync for RiLoadBalancer
impl Unpin for RiLoadBalancer
impl UnsafeUnpin for RiLoadBalancer
impl UnwindSafe for RiLoadBalancer
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FitForCBox for T
impl<T> FitForCBox for T
type CBoxWrapped = Box_<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§impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
impl<'py, T> IntoPyObjectExt<'py> for Twhere
T: IntoPyObject<'py>,
§fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
fn into_bound_py_any(self, py: Python<'py>) -> Result<Bound<'py, PyAny>, PyErr>
self into an owned Python object, dropping type information.§fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
fn into_py_any(self, py: Python<'py>) -> Result<Py<PyAny>, PyErr>
self into an owned Python object, dropping type information and unbinding it
from the 'py lifetime.§fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
fn into_pyobject_or_pyerr(self, py: Python<'py>) -> Result<Self::Output, PyErr>
self into a Python object. Read moreSource§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>
T in a tonic::RequestSource§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>
T in a tonic::Request§impl<T> ManuallyDropMut for T
impl<T> ManuallyDropMut for T
type Ret = ManuallyDrop<T>
fn manually_drop_mut<'__>(&'__ mut self) -> &'__ mut ManuallyDrop<T>
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PyErrArguments for T
impl<T> PyErrArguments for T
§impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
impl<T> PyTypeCheck for Twhere
T: PyTypeInfo,
§const NAME: &'static str = T::NAME
const NAME: &'static str = T::NAME
Use ::classinfo_object() instead and format the type name at runtime. Note that using built-in cast features is often better than manual PyTypeCheck usage.
§fn type_check(object: &Bound<'_, PyAny>) -> bool
fn type_check(object: &Bound<'_, PyAny>) -> bool
§fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
fn classinfo_object(py: Python<'_>) -> Bound<'_, PyAny>
isinstance and issubclass function. Read more