Skip to main content

Pool

Struct Pool 

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

连接池核心实现

Implementations§

Source§

impl Pool

Source

pub fn new( config: PoolConfig, factory: Arc<dyn ConnectionFactory>, ) -> Result<Self, PoolError>

创建连接池

L-5 修复:补充示例文档

§示例
use sz_orm_core::pool::{Pool, PoolConfig, PoolConfigBuilder, ConnectionFactory};
use std::sync::Arc;

struct MyFactory;
impl ConnectionFactory for MyFactory {
    // ...
}

let config = PoolConfigBuilder::new()
    .max_size(10)
    .acquire_timeout(std::time::Duration::from_secs(30))
    .build();
let pool = Pool::new(config, Arc::new(MyFactory))?;
Source

pub fn config(&self) -> &PoolConfig

获取配置

Source

pub async fn acquire(&self) -> Result<PooledConnection, PoolError>

从池中获取连接(带超时)

L-5 修复:补充示例文档

超时时间由 PoolConfig::acquire_timeout 控制,默认 30 秒。 若超时则返回 PoolError::AcquireTimeout

§示例
// 从池中获取连接
let conn = pool.acquire().await?;
// 使用连接执行查询...
// conn.query("SELECT 1").await?;
Source

pub async fn release(&self, pooled: PooledConnection)

释放连接回池中 如果池已关闭或连接已断开,则直接关闭连接而不是放回池中。

接收 PooledConnection 以保留原始 created_at,避免 max_lifetime 在每次归还后被重置(Critical bug fix)。

Source

pub async fn status(&self) -> PoolStatus

获取池状态

Source

pub async fn reap_idle(&self)

回收空闲过久的连接

Source

pub async fn close_all(&self)

关闭所有空闲连接,并标记池为已关闭 注意:已借出未归还的连接不受影响,但归还时会被直接关闭; 同时 close_all 后的新 acquire 也会被拒绝。

Source

pub async fn health_check(&self) -> u32

M-7 修复:连接池健康检查(heartbeat)

对所有空闲连接执行 ping(),移除已断开或 ping 失败的连接。 调用方应定期调用此方法(如每 60 秒),以清理失效连接。

§返回值

返回被移除的连接数。

§注意
  • 此方法会持锁等待所有 ping 完成,可能阻塞 acquire/release
  • 仅检查空闲连接,不影响已借出的连接
  • 对于大量空闲连接,可能产生较多并发 ping,建议在低峰期执行

Auto Trait Implementations§

§

impl !Freeze for Pool

§

impl !RefUnwindSafe for Pool

§

impl !UnwindSafe for Pool

§

impl Send for Pool

§

impl Sync for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

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