pub struct PgConnectionManager<Tls> { /* private fields */ }
Expand description

An mobc::Manager for tokio_postgres::Clients.

Example

use mobc::Pool;
use std::str::FromStr;
use std::time::Instant;
use mobc_postgres::PgConnectionManager;
use tokio_postgres::Config;
use tokio_postgres::NoTls;

#[tokio::main]
async fn main() {
    let config = Config::from_str("postgres://jiaju:jiaju@localhost:5432").unwrap();
    let manager = PgConnectionManager::new(config, NoTls);
    let pool = Pool::builder().max_open(20).build(manager);
    const MAX: usize = 5000;

    let now = Instant::now();
    let (tx, mut rx) = tokio::sync::mpsc::channel::<usize>(16);
    for i in 0..MAX {
        let pool = pool.clone();
        let mut tx_c = tx.clone();
        tokio::spawn(async move {
            let client = pool.get().await.unwrap();
            let rows = client.query("SELECT 1 + 2", &[]).await.unwrap();
            let value: i32 = rows[0].get(0);
            assert_eq!(value, 3);
            tx_c.send(i).await.unwrap();
        });
    }
    for _ in 0..MAX {
        rx.recv().await.unwrap();
    }

    println!("cost: {:?}", now.elapsed());
}

Implementations§

Trait Implementations§

The connection type this manager deals with.
The error type returned by Connections.
Attempts to create a new connection.
Determines if the connection is still connected to the database when check-out. Read more
Spawns a new asynchronous task.
Quickly determines a connection is still valid when check-in.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more