Skip to main content

ReadWriteRouter

Struct ReadWriteRouter 

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

读写分离路由器

master 处理写请求,slave 集群处理读请求。 根据 LoadBalanceStrategy 在多个 slave 间分配读请求。

Implementations§

Source§

impl ReadWriteRouter

Source

pub fn new(master: &str, slaves: Vec<&str>) -> Self

Source

pub fn master(&self) -> &str

返回 master 节点

Source

pub fn slaves(&self) -> &[String]

返回 slave 列表

Source

pub fn slave(&self) -> &str

根据当前策略选择一个 slave

如果 slaves 为空,回退到 master。 如果启用健康检查,会跳过 Unhealthy/Drained 的 slave。

Source

pub fn set_strategy(&mut self, strategy: LoadBalanceStrategy)

设置负载均衡策略

Source

pub fn strategy(&self) -> LoadBalanceStrategy

获取当前策略

Source

pub fn set_weight(&self, slave: &str, weight: u32) -> Result<(), String>

设置 slave 权重

Source

pub fn weight(&self, slave: &str) -> Option<u32>

获取 slave 权重

Source

pub fn health_checker(&self) -> &HealthChecker

健康检查器引用

Source

pub fn latency_stats(&self) -> &LatencyStats

延迟统计引用

Source

pub fn record_latency(&self, slave: &str, latency: Duration)

记录一次 slave 请求的延迟

Source

pub fn measure<F, T>(&self, slave: &str, f: F) -> T
where F: FnOnce() -> T,

测量并记录一次 slave 调用的耗时

Source

pub fn set_read_rationing(&self, percent: u8)

配置读写比例

Source

pub fn read_rationing_percent(&self) -> u8

获取当前读写比例

Source

pub fn acquire(&self, slave: &str) -> Result<(), String>

在某个 slave 上获取连接(增加连接计数,用于 LeastConnections)

Source

pub fn release(&self, slave: &str) -> Result<(), String>

释放某个 slave 的连接(减少连接计数)

Source

pub fn connection_count(&self, slave: &str) -> Option<usize>

查询某个 slave 的当前连接数(用于测试和监控)

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, 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