Pools

Struct Pools 

Source
pub struct Pools {
    pub config: Config,
    pub pool_opts: PoolOpts,
    /* private fields */
}
Expand description

每个 Pools 实例使用独立的连接池,避免不同实例之间的锁竞争 参考 mysql crate 的 Pool 设计

Fields§

§config: Config

配置

§pool_opts: PoolOpts

连接池选项

Implementations§

Source§

impl Pools

Source

pub fn get_guard(&mut self) -> Result<ConnectionGuard<'_>, String>

获取连接守卫(自动管理连接生命周期)

Source

pub fn new(config: Config, pool_opts: PoolOpts) -> Result<Self, String>

创建新的连接池(参考 mysql 的 Pool::new 方式)

§参数
  • config - 数据库配置
  • pool_opts - 连接池选项(可选,默认使用 PoolOpts::default())
§示例
use br_pgsql::pools::Pools;
use br_pgsql::config::{Config, PoolConstraints, PoolOpts};
 
let config = Config::new(&json::object! {
    hostname: "localhost",
    hostport: 5432,
    // ...
})?;
 
let constraints = PoolConstraints::new(0, 400)?;
let pool_opts = PoolOpts::default()
    .with_constraints(constraints);
 
let pools = Pools::new(config, pool_opts)?;
Source

pub fn get_connect(&mut self) -> Result<PgsqlConnect, String>

获取连接 —— 优化:减少锁持有时间,优先复用有效连接,改进等待策略

Source

pub fn release_conn(&self, conn: PgsqlConnect)

把连接放回池子(显式归还),如果连接无效或池子已满则丢弃 优化:使用快速检查,减少锁持有时间,优化池满处理

Source

pub fn idle_pool_size(&self) -> usize

获取当前连接池中的空闲连接数量

Source

pub fn total_connections(&self) -> usize

获取总连接数量(包括已借出的)

Source

pub fn borrowed_connections(&self) -> usize

获取已借出的连接数量

Source

pub fn usage_rate(&self) -> f64

获取连接池使用率(0.0 - 1.0)

Source

pub fn health_status(&self) -> String

获取连接池健康状态

Source

pub fn new_with_size(config: Config, size: usize) -> Result<Self, String>

兼容旧版本的 new 方法(使用 size 参数) 推荐使用 new(config, pool_opts) 方法

Source

pub fn cleanup_idle_connections(&self) -> usize

清除无效连接的方法 返回清理的连接数量 优化:使用快速检查,减少开销

Trait Implementations§

Source§

impl Clone for Pools

Source§

fn clone(&self) -> Pools

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Pools

§

impl RefUnwindSafe for Pools

§

impl Send for Pools

§

impl Sync for Pools

§

impl Unpin for Pools

§

impl UnwindSafe for Pools

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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