Skip to main content

LoadBalancer

Struct LoadBalancer 

Source
pub struct LoadBalancer { /* private fields */ }
Expand description

负载均衡器

Implementations§

Source§

impl LoadBalancer

Source

pub fn new<S>(backends: Backends, selector: S) -> Self
where S: Selector + Send + Sync + 'static, S::Iter: Send + Sync + 'static,

创建负载均衡器实例

Source

pub fn with_health_check( self, health_check: impl HealthCheck + Send + Sync + 'static, ) -> Self

设置健康检查

Source

pub fn with_update_frequency(self, update_frequency: Duration) -> Self

设置更新频率

Source

pub fn with_health_check_frequency( self, health_check_frequency: Duration, ) -> Self

设置健康检查的频率

Source

pub fn with_health_check_parallel(self, health_check_parallel: bool) -> Self

Source

pub async fn update(&self) -> Result<(), Error>

运行服务发现并更新选择算法。

如果这个 LoadBalancer 实例作为后台服务运行,此函数将每隔 update_frequency 被调用一次。

Source

pub fn select(&self, key: &[u8]) -> Option<Backend>

根据选择算法和健康检查结果返回第一个健康的 Backend

key 用于基于哈希的选择,如果选择是随机或轮询,则忽略此参数。

max_iterations 用于限制搜索下一个 Backend 的时间。在某些算法中, 如 Ketama 哈希,搜索下一个后端是线性的,可能需要很多步骤。

Source

pub fn select_with<F>(&self, key: &[u8], accept: F) -> Option<Backend>
where F: Fn(&Backend, bool) -> bool,

类似于 Self::select,根据选择算法和用户定义的 accept 函数返回第一个健康的 Backend

accept 函数接受两个输入:正在选择的后端和该后端的内部健康状态。该函数可以执行一些操作, 比如忽略内部健康检查或因为之前失败而跳过这个后端。accept 函数会被多次调用,遍历后端, 直到返回 true

Trait Implementations§

Source§

impl BackgroundTask for LoadBalancer

Source§

fn run<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

执行任务

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> With for T

Source§

fn with<F>(self, f: F) -> Self
where F: FnOnce(&mut Self),

Source§

fn try_with<F, E>(self, f: F) -> Result<Self, E>
where F: FnOnce(&mut Self) -> Result<(), E>,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more