pub struct Pool { /* private fields */ }Expand description
连接池核心实现
所有字段均为 Arc 或内部含 Arc(Notify、PoolConfig 可 clone),
因此 Pool 可低成本 clone(仅增加引用计数)。PooledConnection 持有
Pool 的 clone 以实现 Drop 自动归还。
Implementations§
Source§impl Pool
impl Pool
Sourcepub fn new(
config: PoolConfig,
factory: Arc<dyn ConnectionFactory>,
) -> Result<Pool, PoolError>
pub fn new( config: PoolConfig, factory: Arc<dyn ConnectionFactory>, ) -> Result<Pool, PoolError>
创建连接池
L-5 修复:补充示例文档
§示例
ⓘ
use sz_orm_pool::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))?;Sourcepub fn config(&self) -> &PoolConfig
pub fn config(&self) -> &PoolConfig
获取配置
Sourcepub fn configure_circuit_breaker(
&self,
failure_threshold: usize,
reset_timeout: Duration,
)
pub fn configure_circuit_breaker( &self, failure_threshold: usize, reset_timeout: Duration, )
Sourcepub fn reset_circuit_breaker(&self) -> bool
pub fn reset_circuit_breaker(&self) -> bool
#88 修复:手动重置断路器到 Closed 状态
用于故障排除后手动恢复,无视当前 reset_timeout 是否到达。 返回是否实际发生了状态变更。
Sourcepub fn circuit_state(&self) -> CircuitState
pub fn circuit_state(&self) -> CircuitState
#88 修复:获取断路器当前状态
Sourcepub async fn acquire(&self) -> Result<PooledConnection, PoolError>
pub async fn acquire(&self) -> Result<PooledConnection, PoolError>
Sourcepub async fn release(&self, pooled: PooledConnection)
pub async fn release(&self, pooled: PooledConnection)
释放连接回池中 如果池已关闭或连接已断开,则直接关闭连接而不是放回池中。
接收 PooledConnection 以保留原始 created_at,避免 max_lifetime
在每次归还后被重置(Critical bug fix)。
显式调用 release 后,pooled.pool 设为 None,避免 Drop 重复归还。
Sourcepub async fn status(&self) -> PoolStatus
pub async fn status(&self) -> PoolStatus
获取池状态
v1.1.0 优化 2:idle 长度从 Mutex::lock().await 改为 ArrayQueue::len()
(原子 load,无任何等待)。该方法保留 async 签名以兼容旧调用方。
Sourcepub async fn close_all(&self)
pub async fn close_all(&self)
关闭所有空闲连接,并标记池为已关闭 注意:已借出未归还的连接不受影响,但归还时会被直接关闭; 同时 close_all 后的新 acquire 也会被拒绝。
Sourcepub async fn health_check(&self) -> u32
pub async fn health_check(&self) -> u32
Sourcepub async fn shutdown(&self)
pub async fn shutdown(&self)
优雅停机:关闭所有空闲连接,等待所有在途连接归还
- 标记池为已关闭(拒绝新 acquire)
- 通知所有等待者(让 acquire 等待者立即返回 Closed 错误)
- 关闭所有空闲连接(立即释放,避免 wait 阶段无意义等待)
- 等待在途(已借出)连接归还(带 30 秒超时)
Sourcepub fn resize(&self, new_max: usize)
pub fn resize(&self, new_max: usize)
动态调整连接池最大容量(resize 的别名,接受 usize)
简化实现:仅更新动态 max_size 值,在 acquire 时检查新值。
- 如果 new_max 大于当前值,允许创建更多连接(受 ArrayQueue 容量限制: 超出原始 max_size 的空闲连接会在 release 时因队列满而被关闭)
- 如果 new_max 小于当前值,不立即关闭多余连接,但阻止新连接创建 (多余连接会在 release/reap_idle 时自然回收)
Sourcepub fn set_max_size(&self, new_max: u32)
pub fn set_max_size(&self, new_max: u32)
动态调整连接池最大容量
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Pool
impl !UnwindSafe for Pool
impl Freeze for Pool
impl Send for Pool
impl Sync for Pool
impl Unpin for Pool
impl UnsafeUnpin for Pool
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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>
Converts
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>
Converts
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