Skip to main content

ConnectionPool

Struct ConnectionPool 

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

LRU 连接池

Implementations§

Source§

impl ConnectionPool

Source

pub fn new(config: PoolConfig) -> Self

Source

pub fn config(&self) -> &PoolConfig

获取配置

Source

pub async fn admit( &self, connection_id: impl Into<String>, now_ms: i64, ) -> AdmitResult

尝试将连接加入池中。

  • 若已存在,返回 AlreadyExists
  • 若池已满,淘汰 LRU 末尾连接后接纳
  • 否则接纳
Source

pub async fn remove(&self, connection_id: &str) -> Option<PooledConnection>

移除连接

Source

pub async fn touch(&self, connection_id: &str, now_ms: i64) -> bool

更新连接活跃时间(移到 LRU 头部)

Source

pub async fn record_sent(&self, connection_id: &str) -> bool

记录发送消息

Source

pub async fn record_received(&self, connection_id: &str) -> bool

记录接收消息

Source

pub async fn get(&self, connection_id: &str) -> Option<PooledConnection>

获取连接

Source

pub async fn count(&self) -> usize

当前连接数

Source

pub async fn is_full(&self) -> bool

是否已满

Source

pub async fn find_by_user(&self, user_id: i64) -> Vec<PooledConnection>

按用户 ID 查询连接

Source

pub async fn evict_idle(&self, idle_threshold_ms: i64, now_ms: i64) -> usize

清理空闲超过指定时长的连接,返回被清理的数量

Source

pub async fn clear(&self)

清空连接池

Source

pub async fn lru_order_list(&self) -> Vec<String>

获取 LRU 顺序的连接 ID 列表(最近活跃在前)

Trait Implementations§

Source§

impl Debug for ConnectionPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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